本文整理汇总了C#中MethodInfo.GetCustomAttributes方法的典型用法代码示例。如果您正苦于以下问题:C# MethodInfo.GetCustomAttributes方法的具体用法?C# MethodInfo.GetCustomAttributes怎么用?C# MethodInfo.GetCustomAttributes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MethodInfo
的用法示例。
在下文中一共展示了MethodInfo.GetCustomAttributes方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CanBeUsedAsAction
public static bool CanBeUsedAsAction(MethodInfo methodInfo, bool ignoreReturnType)
{
if (!methodInfo.IsPublic)
return false;
if (!ignoreReturnType && methodInfo.ReturnType != typeof(IEnumerator<React.NodeResult>))
return false;
if (methodInfo.GetParameters().Length != 0)
{
if (methodInfo.GetCustomAttributes(typeof(React.ReactActionAttribute), true).Length == 0)
return false;
}
return true;
}
示例2: BuildMethodString
public static string BuildMethodString(ref MethodInfo method)
{
string empty = string.Empty;
string str = "no help";
object[] customAttributes = method.GetCustomAttributes(true);
for (int i = 0; i < (int)customAttributes.Length; i++)
{
object obj = customAttributes[i];
if (obj is ConsoleSystem.Help)
{
empty = (obj as ConsoleSystem.Help).argsDescription;
str = (obj as ConsoleSystem.Help).helpDescription;
empty = string.Concat(" ", empty.Trim(), " ");
}
}
return string.Concat(new string[] { method.Name, "(", empty, ") : ", str });
}
示例3: CheckFlags
static void CheckFlags (PropertyInfo pi, MethodInfo accessor)
{
object [] attrs = accessor.GetCustomAttributes (typeof (AccessorCheckAttribute), true);
if (attrs == null)
return;
AccessorCheckAttribute accessor_attr = (AccessorCheckAttribute) attrs [0];
MethodAttributes accessor_flags = accessor.Attributes;
if ((accessor_flags & accessor_attr.Attributes) == accessor_attr.Attributes)
Console.WriteLine ("Test for {0}.{1} PASSED", pi.Name, accessor.Name);
else {
string message = String.Format ("Test for {0}.{1} INCORRECT: MethodAttributes should be {2}, but are {3}",
pi.Name, accessor.Name, accessor_attr.Attributes, accessor_flags);
throw new Exception (message);
}
}
示例4: GetMethodName
static string GetMethodName(MethodInfo md)
{
if (md.Name.StartsWith("op_"))
{
return md.Name;
}
object[] attrs = md.GetCustomAttributes(true);
for (int i = 0; i < attrs.Length; i++)
{
if (attrs[i] is LuaRenameAttribute)
{
LuaRenameAttribute attr = attrs[i] as LuaRenameAttribute;
return attr.Name;
}
}
return md.Name;
}
示例5: ValidateAnnotations
private static void ValidateAnnotations(MethodInfo method, bool noValidate = false)
{
Dictionary<string, ParameterInfo> methodParams = method.GetParameters()
.ToDictionary(m => m.Name);
TeakLinkAttribute[] teakLinks = method.GetCustomAttributes(typeof(TeakLinkAttribute), false) as TeakLinkAttribute[];
foreach(TeakLinkAttribute link in teakLinks)
{
// https://github.com/rkh/mustermann/blob/master/mustermann-simple/lib/mustermann/simple.rb
Regex escape = new Regex(@"[^\?\%\\\/\:\*\w]");
string pattern = escape.Replace(link.Url, (Match m) => {
return Regex.Escape(m.Value);
});
List<string> dupeCheck = new List<string>();
Regex compile = new Regex(@"((:\w+)|\*)");
pattern = compile.Replace(pattern, (Match m) => {
if(m.Value == "*")
{
// 'splat' behavior could be bad to support from a debugging standpoint
throw new NotSupportedException(String.Format("'splat' functionality is not supported by TeakLinks.\nMethod: {0}", DebugStringForMethodInfo(method)));
// return "(?<splat>.*?)";
}
dupeCheck.Add(m.Value.Substring(1));
return String.Format("(?<{0}>[^/?#]+)", m.Value.Substring(1));
});
if(!noValidate)
{
// Check for duplicate capture group names
List<string> duplicates = dupeCheck
.GroupBy(i => i)
.Where(g => g.Count() > 1)
.Select(g => g.Key)
.ToList();
if(duplicates.Count() > 0)
{
throw new ArgumentException(String.Format("Duplicate variable name '{0}'.\nMethod: {1}", duplicates[0], DebugStringForMethodInfo(method)));
}
}
link.Regex = new Regex(pattern);
link.MethodParams = methodParams;
link.MethodInfo = method;
if(!noValidate)
{
// Check for special case where method has only one parameter named 'params' of type Dictionary<string, object>
if(link.MethodParams.ContainsKey("parameters"))
{
if(link.MethodParams["parameters"].ParameterType != typeof(Dictionary<string, object>))
{
throw new ArgumentException(String.Format("Parameter passing by 'parameters' must use type Dictionary<string, object>.\nMethod: {0}", DebugStringForMethodInfo(method)));
}
}
else
{
foreach(string groupName in link.Regex.GetGroupNames())
{
if(groupName == "0") continue;
if(!link.MethodParams.ContainsKey(groupName))
{
throw new ArgumentException(String.Format("TeakLink missing parameter name '{0}'\nMethod: {1}", groupName, DebugStringForMethodInfo(method)));
}
}
}
}
// Check for either static method or method on a MonoBehaviour
if(method.IsStatic)
{
// Nothing for now
}
else if(method.DeclaringType.BaseType == typeof(MonoBehaviour)) // TODO: Walk back base types?
{
link.DeclaringObjectType = method.DeclaringType;
}
else
{
Debug.LogError(method.DeclaringType + " is a " + method.DeclaringType.BaseType);
throw new NotSupportedException(String.Format("Method must be declared 'static' if it is not on a MonoBehaviour.\nMethod: {0}", DebugStringForMethodInfo(method)));
}
if(!noValidate)
{
// Check for duplicate routes
string dupeRouteCheck = link.Url;
foreach(string groupName in link.Regex.GetGroupNames())
{
if(groupName == "0") continue;
dupeRouteCheck = dupeRouteCheck.Replace(String.Format(":{0}", groupName), "");
}
foreach(KeyValuePair<Regex, TeakLinkAttribute> entry in TeakLinkAttribute.Links)
{
string emptyVarRoute = entry.Value.Url;
foreach(string groupName in entry.Key.GetGroupNames())
{
if(groupName == "0") continue;
emptyVarRoute = emptyVarRoute.Replace(String.Format(":{0}", groupName), "");
//.........这里部分代码省略.........
示例6: IsRunnableKoan
private static bool IsRunnableKoan(MethodInfo method)
{
return method.GetCustomAttributes(false).Count(attribute =>
attribute.GetType().FullName == typeof(KoanAttribute).FullName
&& !((KoanAttribute)attribute).Ignore
) != 0;
}
示例7: CheckExceptions
private void CheckExceptions(MethodInfo m)
{
ShouldThrowExceptionAttribute[] attrs = (ShouldThrowExceptionAttribute[])m.GetCustomAttributes(typeof(ShouldThrowExceptionAttribute), true);
ArrayList allowedExceptionTypes = null;
if(attrs.Length > 0) {
allowedExceptionTypes = new ArrayList();
foreach(ShouldThrowExceptionAttribute s in attrs)
allowedExceptionTypes.Add(s.allowedExceptionType);
}
bool wantException = (allowedExceptionTypes != null) && (allowedExceptionTypes.Count > 0);
bool gotException = exceptionThrown != null;
if(wantException && gotException) {
bool gotRightKindOfException = false;
foreach(System.Type et in allowedExceptionTypes) {
if(et.IsAssignableFrom(exceptionThrown.GetType())) {
gotRightKindOfException = true;
break;
}
}
if(!gotRightKindOfException) {
// FAIL.
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("Expected one type of exception, but got another. Expected one of:");
foreach(System.Type tt in allowedExceptionTypes)
sb.Append(" ").Append(tt.Name);
_Fail(sb.ToString(), exceptionThrown);
}
} else if(wantException && !gotException) {
// FAIL.
_Fail("Expected an exception, but did not get one.");
} else if(!wantException && gotException) {
// FAIL.
_Fail("Unexpected exception.", exceptionThrown);
}
}
示例8: CheckPermission
private static void CheckPermission(List<ReadonlyItem> inspectorOwnedAction, List<MethodInfo> invalidActions, MethodInfo method)
{
var permission = inspectorOwnedAction.Find(x => x.ActionName.EqualsOrdinalIgnoreCase(method.Name));
if (permission != null)
{
if (permission.HttpMethod != null)
{
var currentMethodAttributes = method.GetCustomAttributes(false).ToList();
var isInvalid = currentMethodAttributes.Find(x => x.ToString().EqualsOrdinalIgnoreCase(permission.HttpMethod.FullName)) == null;
if (isInvalid) invalidActions.Add(method);
}
}
else
{
invalidActions.Add(method);
}
}
示例9: ProcessDefinition
public bool ProcessDefinition(MethodInfo method, out object instance)
{
instance = null;
string objectName = method.Name;
if (objectName.StartsWith("set_") || objectName.StartsWith("get_"))
{
return false;
}
object[] attribs = method.GetCustomAttributes(typeof(ObjectDefAttribute), true);
if (attribs.Length == 0)
{
return false;
}
if (this._configurableListableObjectFactory.IsCurrentlyInCreation(objectName))
{
Logger.Debug(m => m("Object '{0}' currently in creation, created one", objectName));
return false;
}
Logger.Debug(m => m("Object '{0}' not in creation, asked the application context for one", objectName));
instance = this._configurableListableObjectFactory.GetObject(objectName);
return true;
}
示例10: GetMethodAttributes
/// <summary>
/// Calculates and returns the list of attributes that apply to the
/// specified method.
/// </summary>
/// <param name="method">The method to find attributes for.</param>
/// <returns>
/// A list of custom attributes that should be applied to method.
/// </returns>
/// <see cref="IProxyTypeBuilder.ProxyTargetAttributes"/>
/// <see cref="IProxyTypeBuilder.MemberAttributes"/>
protected virtual IList GetMethodAttributes(MethodInfo method)
{
ArrayList attributes = new ArrayList();
if (this.ProxyTargetAttributes)
{
// add attributes that apply to the target method
#if NET_2_0
object[] attrs = method.GetCustomAttributes(false);
try
{
System.Collections.Generic.IList<CustomAttributeData> attrsData =
CustomAttributeData.GetCustomAttributes(method);
if (attrs.Length != attrsData.Count)
{
// http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94803
attributes.AddRange(attrs);
}
else
{
foreach (CustomAttributeData cad in attrsData)
{
attributes.Add(cad);
}
}
}
catch (ArgumentException)
{
// http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=296032
attributes.AddRange(attrs);
}
#else
attributes.AddRange(method.GetCustomAttributes(false));
#endif
}
// add attributes defined by configuration
foreach (DictionaryEntry entry in MemberAttributes)
{
if (TypeResolutionUtils.MethodMatch((string)entry.Key, method))
{
if (entry.Value is Attribute)
{
attributes.Add(entry.Value);
}
else if (entry.Value is IList)
{
attributes.AddRange(entry.Value as IList);
}
}
}
return attributes;
}