本文整理汇总了C#中IReflect.GetMember方法的典型用法代码示例。如果您正苦于以下问题:C# IReflect.GetMember方法的具体用法?C# IReflect.GetMember怎么用?C# IReflect.GetMember使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IReflect
的用法示例。
在下文中一共展示了IReflect.GetMember方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetAndWrapMember
private static MemberInfo[] GetAndWrapMember(IReflect reflect, Object namedItem, String name, BindingFlags bindingAttr){
PropertyInfo property = reflect.GetProperty(name, bindingAttr);
if (property != null){
MethodInfo getMethod = JSProperty.GetGetMethod(property, false);
MethodInfo setMethod = JSProperty.GetSetMethod(property, false);
if ((getMethod != null && !getMethod.IsStatic) || (setMethod != null && !setMethod.IsStatic)){
MethodInfo method = reflect.GetMethod(name, bindingAttr);
if (method != null && !method.IsStatic){
MemberInfo[] propMethods = new MemberInfo[1];
propMethods[0] = new JSWrappedPropertyAndMethod(property, method, namedItem);
return propMethods;
}
}
}
MemberInfo[] members = reflect.GetMember(name, bindingAttr);
if (members != null && members.Length > 0)
return ScriptObject.WrapMembers(members, namedItem);
return null;
}
示例2: GetAndWrapMember
private static MemberInfo[] GetAndWrapMember(IReflect reflect, object namedItem, string name, BindingFlags bindingAttr)
{
PropertyInfo property = reflect.GetProperty(name, bindingAttr);
if (property != null)
{
MethodInfo getMethod = JSProperty.GetGetMethod(property, false);
MethodInfo setMethod = JSProperty.GetSetMethod(property, false);
if (((getMethod != null) && !getMethod.IsStatic) || ((setMethod != null) && !setMethod.IsStatic))
{
MethodInfo method = reflect.GetMethod(name, bindingAttr);
if ((method != null) && !method.IsStatic)
{
return new MemberInfo[] { new JSWrappedPropertyAndMethod(property, method, namedItem) };
}
}
}
MemberInfo[] member = reflect.GetMember(name, bindingAttr);
if ((member != null) && (member.Length > 0))
{
return ScriptObject.WrapMembers(member, namedItem);
}
return null;
}
示例3: setMember
/*
* Writes to fields or properties, either static or instance. Throws an error
* if the operation is invalid.
*/
private int setMember(IntPtr luaState, IReflect targetType, object target, BindingFlags bindingType)
{
string fieldName = LuaDLL.lua_tostring(luaState, 2);
if (fieldName == null)
{
translator.throwError(luaState, "field or property does not exist");
return 0;
}
MemberInfo member = (MemberInfo)checkMemberCache(memberCache, targetType, fieldName);
if (member == null)
{
MemberInfo[] members = targetType.GetMember(fieldName, bindingType | BindingFlags.Public | BindingFlags.NonPublic);
if (members.Length > 0)
{
member = members[0];
setMemberCache(memberCache, targetType, fieldName, member);
}
else
{
translator.throwError(luaState, "field or property does not exist");
return 0;
}
}
if (member.MemberType == MemberTypes.Field)
{
FieldInfo field = (FieldInfo)member;
object val = translator.getAsType(luaState, 3, field.FieldType);
try
{
field.SetValue(target, val);
}
catch (Exception e)
{
translator.throwError(luaState, e);
}
return 0;
}
else if (member.MemberType == MemberTypes.Property)
{
PropertyInfo property = (PropertyInfo)member;
object val = translator.getAsType(luaState, 3, property.PropertyType);
try
{
property.SetValue(target, val, null);
}
catch (Exception e)
{
translator.throwError(luaState, e);
}
return 0;
}
translator.throwError(luaState, "field or property does not exist");
return 0;
}
示例4: getMember
/*
* Pushes the value of a member or a delegate to call it, depending on the type of
* the member. Works with static or instance members.
* Uses reflection to find members, and stores the reflected MemberInfo object in
* a cache (indexed by the type of the object and the name of the member).
*/
private int getMember(IntPtr luaState, IReflect objType, object obj, string methodName, BindingFlags bindingType)
{
bool implicitStatic = false;
MemberInfo member = null;
object cachedMember = checkMemberCache(memberCache, objType, methodName);
//object cachedMember=null;
if (cachedMember is LuaCSFunction)
{
translator.pushFunction(luaState, (LuaCSFunction)cachedMember);
translator.push(luaState, true);
return 2;
}
else if (cachedMember != null)
{
member = (MemberInfo)cachedMember;
}
else
{
MemberInfo[] members = objType.GetMember(methodName, bindingType | BindingFlags.Public | BindingFlags.NonPublic);
if (members.Length > 0)
member = members[0];
else
{
// If we can't find any suitable instance members, try to find them as statics - but we only want to allow implicit static
// lookups for fields/properties/events -kevinh
members = objType.GetMember(methodName, bindingType | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
if (members.Length > 0)
{
member = members[0];
implicitStatic = true;
}
}
}
if (member != null)
{
if (member.MemberType == MemberTypes.Field)
{
FieldInfo field = (FieldInfo)member;
if (cachedMember == null) setMemberCache(memberCache, objType, methodName, member);
try
{
translator.push(luaState, field.GetValue(obj));
}
catch
{
LuaDLL.lua_pushnil(luaState);
}
}
else if (member.MemberType == MemberTypes.Property)
{
PropertyInfo property = (PropertyInfo)member;
if (cachedMember == null) setMemberCache(memberCache, objType, methodName, member);
try
{
object val = property.GetValue(obj, null);
translator.push(luaState, val);
}
catch (ArgumentException)
{
// If we can't find the getter in our class, recurse up to the base class and see
// if they can help.
if (objType is Type && !(((Type)objType) == typeof(object)))
return getMember(luaState, ((Type)objType).BaseType, obj, methodName, bindingType);
else
LuaDLL.lua_pushnil(luaState);
}
catch
{
LuaDLL.lua_pushnil(luaState);
}
}
else if (member.MemberType == MemberTypes.Event)
{
EventInfo eventInfo = (EventInfo)member;
if (cachedMember == null) setMemberCache(memberCache, objType, methodName, member);
translator.push(luaState, new RegisterEventHandler(translator.pendingEvents, obj, eventInfo));
}
else if(!implicitStatic)
{
if (member.MemberType == MemberTypes.NestedType)
{
// kevinh - added support for finding nested types
// cache us
if (cachedMember == null) setMemberCache(memberCache, objType, methodName, member);
// Find the name of our class
string name = member.Name;
Type dectype = member.DeclaringType;
// Build a new long name and try to find the type by name
//.........这里部分代码省略.........
示例5: isMemberPresent
/// <summary>
/// Does this method exist as either an instance or static?
/// </summary>
/// <param name="objType"></param>
/// <param name="methodName"></param>
/// <returns></returns>
bool isMemberPresent(IReflect objType, string methodName)
{
object cachedMember = checkMemberCache(memberCache, objType, methodName);
if (cachedMember != null)
return true;
MemberInfo[] members = objType.GetMember(methodName, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
return (members.Length > 0);
}
示例6: TrySetMember
/// <summary>
/// Tries to set a named property or field
/// </summary>
/// <param name="luaState"></param>
/// <param name="targetType"></param>
/// <param name="target"></param>
/// <param name="bindingType"></param>
/// <returns>false if unable to find the named member, true for success</returns>
private bool TrySetMember (LuaState luaState, IReflect targetType, object target, BindingFlags bindingType, out string detailMessage)
{
detailMessage = null; // No error yet
// If not already a string just return - we don't want to call tostring - which has the side effect of
// changing the lua typecode to string
// Note: We don't use isstring because the standard lua C isstring considers either strings or numbers to
// be true for isstring.
if (LuaLib.LuaType (luaState, 2) != LuaTypes.String) {
detailMessage = "property names must be strings";
return false;
}
// We only look up property names by string
string fieldName = LuaLib.LuaToString (luaState, 2).ToString ();
if (fieldName == null || fieldName.Length < 1 || !(char.IsLetter (fieldName [0]) || fieldName [0] == '_')) {
detailMessage = "invalid property name";
return false;
}
// Find our member via reflection or the cache
var member = (MemberInfo)CheckMemberCache (memberCache, targetType, fieldName);
if (member == null) {
var members = targetType.GetMember (fieldName, bindingType | BindingFlags.Public);
if (members.Length > 0) {
member = members [0];
SetMemberCache (memberCache, targetType, fieldName, member);
} else {
detailMessage = "field or property '" + fieldName + "' does not exist";
return false;
}
}
if (member.MemberType == MemberTypes.Field) {
var field = (FieldInfo)member;
object val = translator.GetAsType (luaState, 3, field.FieldType);
try {
field.SetValue (target, val);
} catch (Exception e) {
ThrowError (luaState, e);
}
// We did a call
return true;
} else if (member.MemberType == MemberTypes.Property) {
var property = (PropertyInfo)member;
object val = translator.GetAsType (luaState, 3, property.PropertyType);
try {
property.SetValue (target, val, null);
} catch (Exception e) {
ThrowError (luaState, e);
}
// We did a call
return true;
}
detailMessage = "'" + fieldName + "' is not a .net field or property";
return false;
}
示例7: GetToXXXXMethod
private static MethodInfo GetToXXXXMethod(IReflect ir, Type desiredType, bool explicitOK){
if ((ir is TypeBuilder) || (ir is EnumBuilder)) return null;
MemberInfo[] members = ir.GetMember(explicitOK ? "op_Explicit" : "op_Implicit", BindingFlags.Public|BindingFlags.Static);
if (members != null)
foreach(MemberInfo mem in members)
if (mem is MethodInfo)
if (((MethodInfo)mem).ReturnType == desiredType)
return (MethodInfo)mem;
return null;
}
示例8: IsMemberPresent
/// <summary>
/// Does this method exist as either an instance or static?
/// </summary>
/// <param name="objType"></param>
/// <param name="methodName"></param>
/// <returns></returns>
bool IsMemberPresent (IReflect objType, string methodName)
{
object cachedMember = CheckMemberCache (memberCache, objType, methodName);
if (cachedMember != null)
return true;
//CP: Removed NonPublic binding search
var members = objType.GetMember (methodName, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
return (members.Length > 0);
}
示例9: GetMethodsByName
private MethodBase[] GetMethodsByName(Type objType, IReflect objIReflect, string name, BindingFlags invokeAttr)
{
int num3;
MemberInfo[] nonGenericMembers = LateBinding.GetNonGenericMembers(objIReflect.GetMember(name, invokeAttr));
if (nonGenericMembers == null)
{
return null;
}
int upperBound = nonGenericMembers.GetUpperBound(0);
for (int i = 0; i <= upperBound; i++)
{
MemberInfo info = nonGenericMembers[i];
if (info != null)
{
Type declaringType;
if (info.MemberType == MemberTypes.Field)
{
declaringType = info.DeclaringType;
int num12 = nonGenericMembers.GetUpperBound(0);
for (int k = 0; k <= num12; k++)
{
if (((i != k) && (nonGenericMembers[k] != null)) && declaringType.IsSubclassOf(nonGenericMembers[k].DeclaringType))
{
nonGenericMembers[k] = null;
num3++;
}
}
}
else
{
MethodInfo getMethod;
if (info.MemberType == MemberTypes.Method)
{
getMethod = (MethodInfo) info;
if (!getMethod.IsHideBySig && ((!getMethod.IsVirtual || (getMethod.IsVirtual && ((getMethod.Attributes & MethodAttributes.NewSlot) != MethodAttributes.PrivateScope))) || (getMethod.IsVirtual && ((getMethod.GetBaseDefinition().Attributes & MethodAttributes.NewSlot) != MethodAttributes.PrivateScope))))
{
declaringType = info.DeclaringType;
int num13 = nonGenericMembers.GetUpperBound(0);
for (int m = 0; m <= num13; m++)
{
if (((i != m) && (nonGenericMembers[m] != null)) && declaringType.IsSubclassOf(nonGenericMembers[m].DeclaringType))
{
nonGenericMembers[m] = null;
num3++;
}
}
}
}
else if (info.MemberType == MemberTypes.Property)
{
PropertyInfo info3 = (PropertyInfo) info;
int num7 = 1;
do
{
if (num7 == 1)
{
getMethod = info3.GetGetMethod();
}
else
{
getMethod = info3.GetSetMethod();
}
if (((getMethod != null) && !getMethod.IsHideBySig) && (!getMethod.IsVirtual || (getMethod.IsVirtual && ((getMethod.Attributes & MethodAttributes.NewSlot) != MethodAttributes.PrivateScope))))
{
declaringType = info.DeclaringType;
int num14 = nonGenericMembers.GetUpperBound(0);
for (int n = 0; n <= num14; n++)
{
if (((i != n) && (nonGenericMembers[n] != null)) && declaringType.IsSubclassOf(nonGenericMembers[n].DeclaringType))
{
nonGenericMembers[n] = null;
num3++;
}
}
}
num7++;
}
while (num7 <= 2);
if ((invokeAttr & BindingFlags.GetProperty) != BindingFlags.Default)
{
getMethod = info3.GetGetMethod();
}
else if ((invokeAttr & BindingFlags.SetProperty) != BindingFlags.Default)
{
getMethod = info3.GetSetMethod();
}
else
{
getMethod = null;
}
if (getMethod == null)
{
num3++;
}
nonGenericMembers[i] = getMethod;
}
else if (info.MemberType == MemberTypes.NestedType)
{
declaringType = info.DeclaringType;
int num15 = nonGenericMembers.GetUpperBound(0);
//.........这里部分代码省略.........
示例10: trySetMember
private bool trySetMember(IntPtr luaState, IReflect targetType, object target, BindingFlags bindingType, out string detailMessage)
{
detailMessage = null;
if (LuaDLL.lua_type(luaState, 2) != LuaTypes.LUA_TSTRING)
{
detailMessage = "property names must be strings";
return false;
}
string memberName = LuaDLL.lua_tostring(luaState, 2);
if (((memberName == null) || (memberName.Length < 1)) || (!char.IsLetter(memberName[0]) && (memberName[0] != '_')))
{
detailMessage = "invalid property name";
return false;
}
MemberInfo member = (MemberInfo) this.checkMemberCache(this.memberCache, targetType, memberName);
if (member == null)
{
MemberInfo[] infoArray = targetType.GetMember(memberName, (bindingType | BindingFlags.Public) | BindingFlags.IgnoreCase);
if (infoArray.Length <= 0)
{
detailMessage = "field or property '" + memberName + "' does not exist";
return false;
}
member = infoArray[0];
this.setMemberCache(this.memberCache, targetType, memberName, member);
}
if (member.MemberType == MemberTypes.Field)
{
FieldInfo info2 = (FieldInfo) member;
object obj2 = this.translator.getAsType(luaState, 3, info2.FieldType);
try
{
info2.SetValue(target, obj2);
}
catch (Exception exception)
{
this.ThrowError(luaState, exception);
}
return true;
}
if (member.MemberType == MemberTypes.Property)
{
PropertyInfo info3 = (PropertyInfo) member;
object obj3 = this.translator.getAsType(luaState, 3, info3.PropertyType);
try
{
info3.SetValue(target, obj3, null);
}
catch (Exception exception2)
{
this.ThrowError(luaState, exception2);
}
return true;
}
detailMessage = "'" + memberName + "' is not a .net field or property";
return false;
}
示例11: isMemberPresent
private bool isMemberPresent(IReflect objType, string methodName)
{
return ((this.checkMemberCache(this.memberCache, objType, methodName) != null) || (objType.GetMember(methodName, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.IgnoreCase).Length > 0));
}
示例12: getMember
private int getMember(IntPtr luaState, IReflect objType, object obj, string methodName, BindingFlags bindingType)
{
bool flag = false;
MemberInfo member = null;
object obj2 = this.checkMemberCache(this.memberCache, objType, methodName);
if (obj2 is LuaCSFunction)
{
this.translator.pushFunction(luaState, (LuaCSFunction) obj2);
this.translator.push(luaState, true);
return 2;
}
if (obj2 != null)
{
member = (MemberInfo) obj2;
}
else
{
MemberInfo[] infoArray = objType.GetMember(methodName, (bindingType | BindingFlags.Public) | BindingFlags.IgnoreCase);
if (infoArray.Length > 0)
{
member = infoArray[0];
}
else
{
infoArray = objType.GetMember(methodName, ((bindingType | BindingFlags.Static) | BindingFlags.Public) | BindingFlags.IgnoreCase);
if (infoArray.Length > 0)
{
member = infoArray[0];
flag = true;
}
}
}
if (member != null)
{
if (member.MemberType == MemberTypes.Field)
{
FieldInfo info2 = (FieldInfo) member;
if (obj2 == null)
{
this.setMemberCache(this.memberCache, objType, methodName, member);
}
try
{
this.translator.push(luaState, info2.GetValue(obj));
}
catch
{
LuaDLL.lua_pushnil(luaState);
}
}
else if (member.MemberType == MemberTypes.Property)
{
PropertyInfo info3 = (PropertyInfo) member;
if (obj2 == null)
{
this.setMemberCache(this.memberCache, objType, methodName, member);
}
try
{
object o = info3.GetValue(obj, null);
this.translator.push(luaState, o);
}
catch (ArgumentException)
{
if ((objType is Type) && (((Type) objType) != typeof(object)))
{
return this.getMember(luaState, ((Type) objType).BaseType, obj, methodName, bindingType);
}
LuaDLL.lua_pushnil(luaState);
}
catch (TargetInvocationException exception)
{
this.ThrowError(luaState, exception);
LuaDLL.lua_pushnil(luaState);
}
}
else if (member.MemberType == MemberTypes.Event)
{
EventInfo eventInfo = (EventInfo) member;
if (obj2 == null)
{
this.setMemberCache(this.memberCache, objType, methodName, member);
}
this.translator.push(luaState, new RegisterEventHandler(this.translator.pendingEvents, obj, eventInfo));
}
else if (!flag)
{
if (member.MemberType != MemberTypes.NestedType)
{
LuaCSFunction function = new LuaCSFunction(new LuaMethodWrapper(this.translator, objType, methodName, bindingType).call);
if (obj2 == null)
{
this.setMemberCache(this.memberCache, objType, methodName, function);
}
this.translator.pushFunction(luaState, function);
this.translator.push(luaState, true);
return 2;
}
if (obj2 == null)
{
//.........这里部分代码省略.........
示例13: GetDefaultMembers
private static MemberInfo[] GetDefaultMembers(Type typ, IReflect objIReflect, ref string DefaultName)
{
MemberInfo[] nonGenericMembers;
if (typ == objIReflect)
{
do
{
object[] customAttributes = typ.GetCustomAttributes(typeof(DefaultMemberAttribute), false);
if ((customAttributes != null) && (customAttributes.Length != 0))
{
DefaultName = ((DefaultMemberAttribute) customAttributes[0]).MemberName;
nonGenericMembers = GetNonGenericMembers(typ.GetMember(DefaultName, BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.IgnoreCase));
if ((nonGenericMembers != null) && (nonGenericMembers.Length != 0))
{
return nonGenericMembers;
}
DefaultName = "";
return null;
}
typ = typ.BaseType;
}
while (typ != null);
DefaultName = "";
return null;
}
nonGenericMembers = GetNonGenericMembers(objIReflect.GetMember("", BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.IgnoreCase));
if ((nonGenericMembers == null) || (nonGenericMembers.Length == 0))
{
DefaultName = "";
return null;
}
DefaultName = nonGenericMembers[0].Name;
return nonGenericMembers;
}
示例14: GetMostDerivedMemberInfo
private static MemberInfo GetMostDerivedMemberInfo(IReflect objIReflect, string name, BindingFlags flags)
{
MemberInfo[] nonGenericMembers = GetNonGenericMembers(objIReflect.GetMember(name, flags));
if ((nonGenericMembers == null) || (nonGenericMembers.Length == 0))
{
return null;
}
MemberInfo info2 = nonGenericMembers[0];
int upperBound = nonGenericMembers.GetUpperBound(0);
for (int i = 1; i <= upperBound; i++)
{
if (nonGenericMembers[i].DeclaringType.IsSubclassOf(info2.DeclaringType))
{
info2 = nonGenericMembers[i];
}
}
return info2;
}
示例15: GetMembersByName
private static MemberInfo[] GetMembersByName(IReflect objIReflect, string name, BindingFlags flags)
{
MemberInfo[] nonGenericMembers = GetNonGenericMembers(objIReflect.GetMember(name, flags));
if ((nonGenericMembers != null) && (nonGenericMembers.Length == 0))
{
return null;
}
return nonGenericMembers;
}