本文整理匯總了C#中BindingFlags.HasFlag方法的典型用法代碼示例。如果您正苦於以下問題:C# BindingFlags.HasFlag方法的具體用法?C# BindingFlags.HasFlag怎麽用?C# BindingFlags.HasFlag使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類BindingFlags
的用法示例。
在下文中一共展示了BindingFlags.HasFlag方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: TestAccessibility
private static bool TestAccessibility(MethodBase member, BindingFlags bindingFlags) {
bool visibility = (member.IsPublic && bindingFlags.HasFlag(BindingFlags.Public)) ||
(!member.IsPublic && bindingFlags.HasFlag(BindingFlags.NonPublic));
bool instance = (member.IsStatic && bindingFlags.HasFlag(BindingFlags.Static)) ||
(!member.IsStatic && bindingFlags.HasFlag(BindingFlags.Instance));
return visibility && instance;
}
示例2: AreBindingFlagsMatching
private static bool AreBindingFlagsMatching(FieldInfo fieldInfo, BindingFlags bindingAttr)
{
var publicFlag = bindingAttr.HasFlag(BindingFlags.Public);
var nonPublicFlag = bindingAttr.HasFlag(BindingFlags.NonPublic);
var staticFlag = bindingAttr.HasFlag(BindingFlags.Static);
var instanceFlag = bindingAttr.HasFlag(BindingFlags.Instance);
return ((fieldInfo.IsPublic && publicFlag) || (!fieldInfo.IsPublic && nonPublicFlag)) &&
((fieldInfo.IsStatic && staticFlag) || (!fieldInfo.IsStatic && instanceFlag));
}
示例3: GetMembers
public static MemberInfo[] GetMembers(this Type t, BindingFlags flags)
{
#if NETFX_CORE
if (!flags.HasFlag(BindingFlags.Instance) && !flags.HasFlag(BindingFlags.Static)) return null;
var ti = t.GetTypeInfo();
var result = new List<MemberInfo>();
result.AddRange(ti.DeclaredMembers);
return result.ToArray();
#else
throw new NotImplementedException();
#endif
}
示例4: GetProperties
public static IEnumerable<PropertyInfo> GetProperties(this Type type, BindingFlags bindingFlags) {
IList<PropertyInfo> properties = (bindingFlags.HasFlag(BindingFlags.DeclaredOnly))
? type.GetTypeInfo().DeclaredProperties.ToList()
: type.GetTypeInfo().GetPropertiesRecursive();
return properties.Where(p => TestAccessibility(p, bindingFlags));
}
示例5: GetMethod
public static MethodInfo GetMethod(this Type This, string name, BindingFlags flags = default(BindingFlags))
{
var ti = This.GetTypeInfo();
var ret = ti.GetDeclaredMethod(name);
if (ret != null || !flags.HasFlag(BindingFlags.FlattenHierarchy) || ti.BaseType == null) return ret;
return ti.BaseType.GetMethod(name, flags);
}
示例6: GetMethod
public static MethodInfo GetMethod(this Type This, string methodName, Type[] paramTypes, BindingFlags flags = default(BindingFlags))
{
var ti = This.GetTypeInfo();
var ret = ti.GetDeclaredMethods(methodName)
.FirstOrDefault(x => {
return paramTypes.Zip(x.GetParameters().Select(y => y.ParameterType), (l, r) => l == r).All(y => y != false);
});
if (ret != null || !flags.HasFlag(BindingFlags.FlattenHierarchy) || ti.BaseType == null) return ret;
return ti.BaseType.GetMethod(methodName, paramTypes, flags);
}
示例7: InvokeInternal
protected virtual object InvokeInternal(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, System.Globalization.CultureInfo culture)
{
if (invokeAttr.HasFlag(BindingFlags.GetProperty))
{
var prop = GetProperty(name, invokeAttr);
if (prop == null)
{
throw new MissingFieldException("ScriptDrivenObject", name);
}
return prop.GetValue(target, null);
}
else if (invokeAttr.HasFlag(BindingFlags.SetProperty))
{
var prop = GetProperty(name, invokeAttr);
if (prop == null)
{
throw new MissingFieldException("ScriptDrivenObject", name);
}
prop.SetValue(target, args[0], invokeAttr, binder, null, culture);
return null;
}
else if (invokeAttr.HasFlag(BindingFlags.InvokeMethod))
{
var method = GetMethod(name, invokeAttr);
if (method == null)
{
throw new MissingMethodException("ScriptDrivenObject", name);
}
return method.Invoke(target, invokeAttr, binder, args, culture);
}
else
{
throw new ArgumentException("No flags for property access or method call");
}
}
示例8: InnerInvokeMember
protected override object InnerInvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target,
object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
{
object[] processedArgs = TypeMappingHelpers.MapToHostType(args);
object result;
if (name == SpecialMemberName.Default && invokeAttr.HasFlag(BindingFlags.CreateInstance))
{
if (_engineMode != JsEngineMode.Classic && processedArgs.Length > 0)
{
processedArgs = processedArgs.Skip(1).ToArray();
}
result = Activator.CreateInstance(_type, processedArgs);
}
else
{
result = InvokeStandardMember(name, invokeAttr, binder, target,
processedArgs, modifiers, culture, namedParameters);
}
return TypeMappingHelpers.MapToScriptType(result, _engineMode);
}
示例9: GetFields
public static FieldInfo[] GetFields(this Type t, BindingFlags flags)
{
#if NETFX_CORE
if (!flags.HasFlag(BindingFlags.Instance) && !flags.HasFlag(BindingFlags.Static)) return null;
var ti = t.GetTypeInfo();
var origFields = ti.DeclaredFields;
var results = new List<FieldInfo>();
foreach (var field in origFields)
{
var isValid = (flags.HasFlag(BindingFlags.Public) && field.IsPublic)
|| (flags.HasFlag(BindingFlags.NonPublic) && !field.IsPublic);
isValid &= (flags.HasFlag(BindingFlags.Static) && field.IsStatic) || (flags.HasFlag(BindingFlags.Instance) && !field.IsStatic);
if (flags.HasFlag(BindingFlags.DeclaredOnly))
isValid &= field.DeclaringType == t;
results.Add(field);
}
return results.ToArray();
#else
throw new NotImplementedException();
#endif
}
示例10: GetConstructors
public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
{
// Do not expose non-public constructors for composition, since
// this is the behavior of all other frameworks (including Microsoft.Composition!)
if (bindingAttr.HasFlag(BindingFlags.NonPublic))
bindingAttr &= ~BindingFlags.NonPublic;
var ctor = base.GetConstructors(bindingAttr).OrderByDescending(c => c.GetParameters().Length).FirstOrDefault();
if (ctor != null)
return new ConstructorInfo[] { new ImportingConstructorInfo(ctor) };
return new ConstructorInfo[0];
}
示例11: TryInvokeObject
public static bool TryInvokeObject(object target, BindingFlags invokeFlags, object[] args, object[] bindArgs, bool tryDynamic, out object result)
{
var hostTarget = target as HostTarget;
if (hostTarget != null)
{
if (hostTarget.TryInvoke(invokeFlags, args, bindArgs, out result))
{
return true;
}
if (hostTarget is HostType)
{
return false;
}
target = hostTarget.InvokeTarget;
tryDynamic = tryDynamic && typeof(IDynamicMetaObjectProvider).IsAssignableFrom(hostTarget.Type);
}
if ((target != null) && invokeFlags.HasFlag(BindingFlags.InvokeMethod))
{
if (target is ScriptItem)
{
target = DelegateFactory.CreateFunc<object>(null, target, args.Length);
}
var del = target as Delegate;
if (del != null)
{
result = InvokeDelegate(del, args);
return true;
}
if (tryDynamic)
{
var dynamicMetaObjectProvider = target as IDynamicMetaObjectProvider;
if (dynamicMetaObjectProvider != null)
{
if (dynamicMetaObjectProvider.GetMetaObject(Expression.Constant(target)).TryInvoke(args, out result))
{
return true;
}
}
}
}
result = null;
return false;
}
示例12: IsConformWithBindingFlags
private static bool IsConformWithBindingFlags(FieldInfo method, BindingFlags bindingFlags)
{
if (method.IsPublic && !bindingFlags.HasFlag(BindingFlags.Public))
return false;
if (method.IsPrivate && !bindingFlags.HasFlag(BindingFlags.NonPublic))
return false;
if (method.IsStatic && !bindingFlags.HasFlag(BindingFlags.Static))
return false;
if (!method.IsStatic && !bindingFlags.HasFlag(BindingFlags.Instance))
return false;
return true;
}
示例13: GetProperties
public static PropertyInfo[] GetProperties(this Type type, BindingFlags flags)
{
var props = type.GetRuntimeProperties ();
return props.Where (p =>
((flags.HasFlag (BindingFlags.Static) == (p.GetMethod != null && p.GetMethod.IsStatic)) ||
(flags.HasFlag (BindingFlags.Instance) == (p.GetMethod != null && !p.GetMethod.IsStatic))
) &&
(flags.HasFlag (BindingFlags.Public) == (p.GetMethod != null && p.GetMethod.IsPublic)
)).ToArray ();
}
示例14: TryInvokeAuxMember
public override bool TryInvokeAuxMember(string memberName, BindingFlags invokeFlags, object[] args, object[] bindArgs, out object result)
{
if (invokeFlags.HasFlag(BindingFlags.InvokeMethod))
{
if (memberName == "get")
{
result = target.InvokeMember(name, BindingFlags.GetProperty, args, bindArgs, null);
return true;
}
if (memberName == "set")
{
result = target.InvokeMember(name, BindingFlags.SetProperty, args, bindArgs, null);
return true;
}
}
result = null;
return false;
}
示例15: GetFields
public static IEnumerable<FieldInfo> GetFields(this Type type, BindingFlags bindingFlags)
{
IList<FieldInfo> fields = (bindingFlags.HasFlag(BindingFlags.DeclaredOnly))
? type.GetTypeInfo().DeclaredFields.ToList()
: type.GetTypeInfo().GetFieldsRecursive();
return fields.Where(f => TestAccessibility(f, bindingFlags)).ToList();
}