本文整理汇总了C#中BindingFlags类的典型用法代码示例。如果您正苦于以下问题:C# BindingFlags类的具体用法?C# BindingFlags怎么用?C# BindingFlags使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BindingFlags类属于命名空间,在下文中一共展示了BindingFlags类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IsMatch
private static bool IsMatch(PropertyInfo propertyInfo, BindingFlags flags)
{
// this methods is heavily used during reflection, so we have traded
// readablility for performance.
var mainMethod = propertyInfo.GetGetMethod() ?? propertyInfo.GetSetMethod();
if (mainMethod == null)
return false;
if (flags == Type.BindFlags.AllMembers || flags == Type.BindFlags.DeclaredMembers)
return true;
bool incStatic = (flags & BindingFlags.Static) == BindingFlags.Static;
bool incInstance = (flags & BindingFlags.Instance) == BindingFlags.Instance;
if (incInstance == incStatic && !incInstance)
return false;
if (incInstance != incStatic)
{
bool isStatic = mainMethod.IsStatic;
if (!((incStatic && isStatic) || (incInstance && !isStatic)))
return false;
}
bool incPublic = (flags & BindingFlags.Public) == BindingFlags.Public;
bool incNonPublic = (flags & BindingFlags.NonPublic) == BindingFlags.NonPublic;
if (incPublic == incNonPublic)
return incPublic;
bool isPublic = mainMethod.IsPublic;
return (incPublic && isPublic) || (incNonPublic && !isPublic);
}
示例2: ReadAllFields
/// <summary>
/// Reads all fields with the specified binding of the object in alphabetical order using reflection
/// </summary>
public void ReadAllFields(object target, BindingFlags flags)
{
if (target == null)
throw new ArgumentNullException("target");
Type tp = target.GetType();
FieldInfo[] fields = tp.GetFields(flags);
NetUtility.SortMembersList(fields);
foreach (FieldInfo fi in fields)
{
object value;
// find read method
MethodInfo readMethod;
if (s_readMethods.TryGetValue(fi.FieldType, out readMethod))
{
// read value
value = readMethod.Invoke(this, null);
// set the value
fi.SetValue(target, value);
}
}
}
示例3: InvokeMember
public Object InvokeMember(String name, BindingFlags flags, Binder binder, Object target,
Object[] args, ParameterModifier[] modifiers, CultureInfo locale, String[] namedParameters){
if ((flags & BindingFlags.CreateInstance) == 0)
//Try to create an instance of the array
return LateBinding.CallValue(this.elementType, args, true, true, null, null, binder, locale, namedParameters);
return Typeob.Array.InvokeMember(name, flags, binder, target, args, modifiers, locale, namedParameters);
}
示例4: CreateInstance
/// <include file='doc\Activator.uex' path='docs/doc[@for="Activator.CreateInstance1"]/*' />
static public Object CreateInstance(Type type,
BindingFlags bindingAttr,
Binder binder,
Object[] args,
CultureInfo culture,
Object[] activationAttributes)
{
if (type == null)
throw new ArgumentNullException("type");
if (type is System.Reflection.Emit.TypeBuilder)
throw new NotSupportedException(Environment.GetResourceString( "NotSupported_CreateInstanceWithTypeBuilder" ));
// If they didn't specify a lookup, then we will provide the default lookup.
if ((bindingAttr & (BindingFlags) LookupMask) == 0)
bindingAttr |= Activator.ConstructorDefault;
try {
RuntimeType rt = (RuntimeType) type.UnderlyingSystemType;
return rt.CreateInstanceImpl(bindingAttr,binder,args,culture,activationAttributes);
}
catch (InvalidCastException) {
throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"),"type");
}
}
示例5: SetValue
public override void SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo locale){
if (obj is StackFrame){
this.field.SetValue(((StackFrame)((StackFrame)obj).engine.ScriptObjectStackTop()).closureInstance, value, invokeAttr, binder, locale);
return;
}
throw new JScriptException(JSError.InternalError); //should never happen
}
示例6: SelectMethod
public override MethodBase SelectMethod(
BindingFlags bindingAttr,
MethodBase[] matchMethods,
Type[] parameterTypes,
ParameterModifier[] modifiers) {
for (int i = 0; i < matchMethods.Length; ++i) {
if (matchMethods[i].IsGenericMethodDefinition == _genericMethodDefinition) {
ParameterInfo[] pis = matchMethods[i].GetParameters();
bool match = (pis.Length == parameterTypes.Length);
for (int j = 0; match && j < pis.Length; ++j) {
if (pis[j].ParameterType == parameterTypes[j])
continue;
if (pis[j].ParameterType.IsGenericParameter)
match = CheckGenericTypeConstraints(pis[j].ParameterType, parameterTypes[j]);
else if (pis[j].ParameterType.IsGenericType && parameterTypes[j].IsGenericType)
match = CompareGenericTypesRecursive(pis[j].ParameterType, parameterTypes[j]);
else
match = false;
}
if (match)
return matchMethods[i];
}
}
return null;
}
示例7: GetMethod
/// <summary>
/// Gets a method.
/// </summary>
/// <param name="t"></param>
/// <param name="methodName"></param>
/// <param name="flags"></param>
/// <param name="p1"></param>
/// <param name="parameterTypes"></param>
/// <param name="p2"></param>
/// <returns></returns>
public static System.Reflection.MethodInfo GetMethod(this Type t, string methodName, BindingFlags flags, object p1, Type[] parameterTypes, object p2)
{
var ti = t.GetTypeInfo();
System.Reflection.MethodInfo result = null;
while (ti != null)
{
var potentials = ti.DeclaredMethods.
Where(r => r.Name == methodName &&
r.GetParameters().Count() == parameterTypes.Count());
foreach (var item in potentials)
{
result = item;
var resultParameters = result.GetParameters();
for (int i = 0; i < resultParameters.Count(); i++)
{
if (resultParameters[i].ParameterType != parameterTypes[i])
{
result = null;
break;
}
}
if (result != null)
break;
}
if (result != null)
break;
if (ti.BaseType == null)
break;
ti = ti.BaseType.GetTypeInfo();
}
return result;
}
示例8: GetProperties
/// <summary>
/// Validate and get the list of properties specified by this attribute on the given type.
/// </summary>
/// <param name="type">clr type on which this attribute must have defined.</param>
/// <param name="inherit">whether we need to inherit this attribute or not.
/// For context types,we need to, since we can have one context dervied from another, and we want to ignore all the properties on the base ones too.
/// For resource types, we don't need to, since we don't want derived types to know about ignore properties of the base type. Also
/// from derived type, you cannot change the definition of the base type.</param>
/// <param name="bindingFlags">binding flags to be used for validating property names.</param>
/// <returns>list of property names specified on IgnoreProperties on the given type.</returns>
internal static IEnumerable<string> GetProperties(Type type, bool inherit, BindingFlags bindingFlags)
{
IgnorePropertiesAttribute[] attributes = (IgnorePropertiesAttribute[])type.GetCustomAttributes(typeof(IgnorePropertiesAttribute), inherit);
Debug.Assert(attributes.Length == 0 || attributes.Length == 1, "There should be atmost one IgnoreProperties specified");
if (attributes.Length == 1)
{
foreach (string propertyName in attributes[0].PropertyNames)
{
if (String.IsNullOrEmpty(propertyName))
{
throw new InvalidOperationException(Strings.IgnorePropertiesAttribute_PropertyNameCannotBeNullOrEmpty);
}
PropertyInfo property = type.GetProperty(propertyName, bindingFlags);
if (property == null)
{
throw new InvalidOperationException(Strings.IgnorePropertiesAttribute_InvalidPropertyName(propertyName, type.FullName));
}
}
return attributes[0].PropertyNames;
}
return WebUtil.EmptyStringArray;
}
示例9: ExecutePosTest
private object[] ExecutePosTest(
CustomAttributeBuilder customAttrBuilder,
MethodAttributes getMethodAttr,
Type returnType,
Type[] paramTypes,
BindingFlags bindingAttr)
{
TypeBuilder myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public);
PropertyBuilder myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName,
PropertyAttributes.HasDefault,
returnType, null);
myPropertyBuilder.SetCustomAttribute(customAttrBuilder);
// Define the "get" accessor method for DynamicPropertyName
MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName,
getMethodAttr, returnType, paramTypes);
ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator();
methodILGenerator.Emit(OpCodes.Ldarg_0);
methodILGenerator.Emit(OpCodes.Ret);
// Map the 'get' method created above to our PropertyBuilder
myPropertyBuilder.SetGetMethod(myMethodBuilder);
Type myType = myTypeBuilder.CreateTypeInfo().AsType();
PropertyInfo myProperty = myType.GetProperty(DynamicPropertyName, bindingAttr);
return myProperty.GetCustomAttributes(false).Select(a => (object)a).ToArray();
}
示例10: SelectMethod
public override MethodBase SelectMethod(
BindingFlags bindingAttr,
MethodBase[] matchMethods,
Type[] parameterTypes,
ParameterModifier[] modifiers)
{
for (int i = 0; i < matchMethods.Length; ++i)
{
if (matchMethods[i].IsGenericMethodDefinition != _genericMethodDefinition)
continue;
ParameterInfo[] pis = matchMethods[i].GetParameters();
bool match = (pis.Length == parameterTypes.Length);
for (int j = 0; match && j < pis.Length; ++j)
{
match = TypeHelper.CompareParameterTypes(pis[j].ParameterType, parameterTypes[j]);
}
if (match)
return matchMethods[i];
}
return null;
}
示例11: BindToMethod
public override MethodBase BindToMethod(
BindingFlags bindingAttr,
MethodBase[] match,
ref object[] args,
ParameterModifier[] modifiers,
CultureInfo culture,
string[] names,
out object state
)
{
for (int i = 0; i < args.Length; i++)
{
if (args[i] is string)
{
args[i] = ChangeType(args[i], typeof(string), culture);
}
if (args[i] is int)
{
args[i] = ChangeType(args[i], typeof(int), culture);
}
if (args[i] is bool)
{
args[i] = ChangeType(args[i], typeof(bool), culture);
}
}
return Type.DefaultBinder.BindToMethod(
bindingAttr,
match,
ref args,
modifiers,
culture,
names,
out state
);
}
示例12: BindToField
public override FieldInfo BindToField(BindingFlags bindAttr, FieldInfo[] match, object value, CultureInfo locale)
{
if (value == null)
{
value = DBNull.Value;
}
int num = 0x7fffffff;
int num2 = 0;
FieldInfo info = null;
Type actual = value.GetType();
int index = 0;
int length = match.Length;
while (index < length)
{
FieldInfo info2 = match[index];
int num5 = TypeDistance(Runtime.TypeRefs, info2.FieldType, actual);
if (num5 < num)
{
num = num5;
info = info2;
num2 = 0;
}
else if (num5 == num)
{
num2++;
}
index++;
}
if (num2 > 0)
{
throw new AmbiguousMatchException();
}
return info;
}
示例13: BindToMethod
public override MethodBase BindToMethod(
BindingFlags bindingAttr,
MethodBase[] match,
ref object[] args,
ParameterModifier[] modifiers,
CultureInfo culture,
string[] names,
out object state)
{
if (match == null)
{
throw new ArgumentNullException("match");
}
// Arguments are not being reordered.
state = null;
// Find a parameter match and return the first method with
// parameters that match the request.
foreach (MethodBase mb in match)
{
ParameterInfo[] parameters = mb.GetParameters();
if (ParametersMatch(parameters, args))
{
return mb;
}
}
return null;
}
示例14: ExecuteMethod
public static object ExecuteMethod(Type type, string methodName, object instance, object[] methodParams, BindingFlags bindingFlags)
{
MethodInfo targetMethod = null;
var methods = type.GetMethods(bindingFlags);
var namedMethods = methods.Where(_ => _.Name == methodName && _.GetParameters().Count() == methodParams.Count());
foreach (var namedMethod in namedMethods)
{
var found = true;
for (var i = 0; i < namedMethod.GetParameters().Count(); i++)
{
if (namedMethod.GetParameters()[i].GetType().IsInstanceOfType(methodParams[i]))
{
found = false;
}
}
if (found)
{
targetMethod = namedMethod;
}
}
return targetMethod == null ? null : targetMethod.Invoke(instance, methodParams);
}
示例15: GetFields
/// <summary>
/// Gets the fields.
/// </summary>
/// <param name="typeInfo">The <see cref="TypeInfo"/>.</param>
/// <param name="bindingFlags">The binding flags.</param>
/// <returns>An array of <see cref="FieldInfo"/>.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="typeInfo"/> is <c>null</c>.</exception>
public static FieldInfo[] GetFields(this TypeInfo typeInfo, BindingFlags bindingFlags)
{
Argument.IsNotNull("typeInfo", typeInfo);
var flattenHierarchy = ShouldFlattenHierarchy(bindingFlags);
var source = flattenHierarchy ? typeInfo.AsType().GetRuntimeFields().ToList() : typeInfo.DeclaredFields.ToList();
var includeStatics = Enum<BindingFlags>.Flags.IsFlagSet(bindingFlags, BindingFlags.Static);
// TODO: This is a fix because static members are not included in FlattenHierarcy, remove when this is fixed in WinRT
if (flattenHierarchy)
{
var baseType = typeInfo.BaseType;
if ((baseType != null) && (baseType != typeof(object)))
{
source.AddRange(from member in GetFields(baseType.GetTypeInfo(), bindingFlags)
where member.IsStatic
select member);
}
}
if (!includeStatics)
{
source = (from x in source
where !x.IsStatic
select x).ToList();
}
return (from x in source
where x.IsStatic == includeStatics
select x).ToArray();
}