private static void GetMethodExt(ref MethodInfo matchingMethod, System.Type type, string name, BindingFlags bindingFlags, params System.Type[] parameterTypes) { // Check all methods with the specified name, including in base classes foreach (MethodInfo methodInfo in type.GetMember(name, MemberTypes.Method, bindingFlags)) { // Check that the parameter counts and types match, with 'loose' matching on generic parameters ParameterInfo[] parameterInfos = methodInfo.GetParameters(); if (parameterInfos.Length == parameterTypes.Length) { int i = 0; for (; i < parameterInfos.Length; ++i) { if (!parameterInfos[i].ParameterType.IsSimilarType(parameterTypes[i])) break; } if (i == parameterInfos.Length) { if (matchingMethod == null) matchingMethod = methodInfo; else throw new AmbiguousMatchException("More than one matching method found!"); } } } }
public AccessorPropertyMapper(System.Type declaringType, string propertyName, Action<string> accesorValueSetter) { PropertyName = propertyName; if (declaringType == null) { throw new ArgumentNullException("declaringType"); } MemberInfo member = null; if (propertyName != null) { member = declaringType.GetMember(propertyName, FieldBindingFlag).FirstOrDefault(); } if (member == null) { accesorValueSetter("none"); canChangeAccessor = false; } else if ((member as FieldInfo) != null) { accesorValueSetter("field"); canChangeAccessor = false; } this.declaringType = declaringType; this.propertyName = propertyName; setAccessor = accesorValueSetter; }
private PathInfo[] GetArraySubProperties(System.Type propertyType, string currentPath) { List<PathInfo> list = new List<PathInfo>(); if (propertyType != typeof(string)) { List<MethodInfo> list2 = new List<MethodInfo>(); MemberInfo[] defaultMembers = null; try { defaultMembers = propertyType.GetDefaultMembers(); } catch (NotImplementedException) { } catch (ArgumentException) { } if ((defaultMembers != null) && (defaultMembers.Length > 0)) { foreach (MemberInfo info in defaultMembers) { if (info is PropertyInfo) { list2.Add((info as PropertyInfo).GetGetMethod()); } } } if (propertyType.IsArray) { MemberInfo[] member = propertyType.GetMember("Get"); if ((member != null) && (member.Length > 0)) { foreach (MemberInfo info2 in member) { if (info2 is MethodInfo) { list2.Add(info2 as MethodInfo); } } } } foreach (MethodInfo info3 in list2) { string str = this.ConstructIndexString(info3); if (str != null) { list.Add(new PathInfo(currentPath + str, info3, info3.ReturnType)); } } } return list.ToArray(); }
public void TestGetPropertyFromStringArrayAndGenericType() { var str = new[] { "BProp", "CProp", "DProp" }; var prop = str.GetMember<A>(); Assert.That(prop.Name, Is.EqualTo("DProp")); Assert.That(prop.DeclaringType, Is.EqualTo(typeof(C))); Assert.That(prop.Type, Is.EqualTo(typeof(D))); }
static void mapper_BeforeMapClass(NHibernate.Mapping.ByCode.IModelInspector modelInspector, System.Type type, NHibernate.Mapping.ByCode.IClassAttributesMapper classCustomizer) { classCustomizer.Cache(cacheMapping => cacheMapping.Usage(NHibernate.Mapping.ByCode.CacheUsage.ReadWrite)); string fullName = type.FullName; // example: Domain.TheProduction+Product string[] fullNameSplit = fullName.Split('+'); string className = fullNameSplit[1]; // Last() skips the other namespace(s) string schemaDomainName = fullNameSplit[0].Split('.').Last(); string schemaName = schemaDomainName.Substring(0, schemaDomainName.Length - "Domain".Length); string sqlServerFullName = schemaName + "." + className; classCustomizer.Table(sqlServerFullName); System.Reflection.MemberInfo mi = type.GetMember(className + "Id", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)[0]; classCustomizer.Id(mi, idMapper => { idMapper.Column(className + "Id"); idMapper.Generator(NHibernate.Mapping.ByCode.Generators.Identity); }); }
public static bool HasMember(System.Type tp, string name, bool includeNonPublic) { const BindingFlags BINDING = BindingFlags.Public | BindingFlags.Instance; const BindingFlags PRIV_BINDING = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly; if (tp == null) return false; if (tp.GetMember(name, BINDING) != null) return true; if(includeNonPublic) { while (tp != null) { if (tp.GetMember(name, PRIV_BINDING) != null) return true; tp = tp.BaseType; } } return false; }
private RubyMethod FindCLRMethod(string methodId, System.Type clrtype) { BindingFlags flags = BindingFlags.Instance | BindingFlags.Public; if (this._type == Type.IClass) { // static methods if (methodId == "new") { if (clrtype.IsSubclassOf(typeof(System.Delegate))) { return new RubyMethod(new DelegateConstructor(clrtype), 0, Access.Public, this); } else { ConstructorInfo[] ci = clrtype.GetConstructors(); if (ci == null || ci.Length == 0) return null; MethodBase[] mi = new MethodBase[ci.Length]; System.Array.Copy(ci, mi, ci.Length); return new RubyMethod(new MultiMethod(mi), -1, Access.Public, this); } } else if (methodId == "allocator") { return new RubyMethod(Methods.rb_class_allocate_instance.singleton, 0, Access.Private, this); } else if (methodId == "[]") { // instantiate a generic type // ruby: type = ns::List[System::Int32] if (clrtype.IsGenericType) { // clrtype is Type`n+Inner but we are looking for Type`n+Inner`n // we need to strip generic arguments from the name, but supply // them to the GenericTypeGetter return new RubyMethod( new GenericTypeGetter( clrtype.Assembly, clrtype.GetGenericTypeDefinition().FullName, clrtype.GetGenericArguments()), -1, Access.Public, this); } else { return new RubyMethod( new GenericTypeGetter(clrtype.Assembly, clrtype.FullName, null), -1, Access.Public, this); } } flags = BindingFlags.Static | BindingFlags.Public; } bool is_setter = false; // methods ending with "=" are expected to be either // field or property setters if (methodId.EndsWith("=")) { is_setter = true; methodId = methodId.Substring(0, methodId.Length - 1); } // default member access, an Indexer in C# if (methodId == "[]") { object[] attributes = clrtype.GetCustomAttributes( typeof(System.Reflection.DefaultMemberAttribute), true); if (attributes.Length > 0) { methodId = ((DefaultMemberAttribute)attributes[0]).MemberName; } } MemberInfo[] members = clrtype.GetMember(methodId, flags); if (members.Length == 0) { // we didn't find a member with the exact name // but we still need to check for nested types with // additional type parameters string genericNestedId = methodId + "`"; foreach (System.Type nested in clrtype.GetNestedTypes(flags)) { if (nested.Name.StartsWith(genericNestedId)) { return new RubyMethod( new ValueMethod( new GenericContainer( clrtype.Assembly, clrtype.Name + "+" + methodId, clrtype.GetGenericArguments())), 0, Access.Public, this); } } return null; } if (members[0] is MethodBase) { if (is_setter) return null; MethodBase[] methods = new MethodBase[members.Length]; System.Array.Copy(members, methods, members.Length); return new RubyMethod(new MultiMethod(methods), -1, Access.Public, this); } if (members[0] is PropertyInfo) { // not all the property overloads may have the getter/setter // we're looking for, so we maintain a count and resize // the methods array later if necessary int count = 0; MethodBase[] methods = new MethodBase[members.Length]; foreach (PropertyInfo pi in members) { MethodInfo method = is_setter ? pi.GetSetMethod() : pi.GetGetMethod(); if (method != null) methods[count++] = method; } if (count == 0) return null; if (count < members.Length) System.Array.Resize(ref methods, count); return new RubyMethod(new MultiMethod(methods), -1, Access.Public, this); } FieldInfo field = members[0] as FieldInfo; if (field != null) { if (is_setter) return new RubyMethod(new FieldSetter(field), 1, Access.Public, this); else return new RubyMethod(new FieldGetter(field), 0, Access.Public, this); } //EventInfo eventinfo = members[0] as EventInfo; //if (eventinfo != null) //{ // return ...; //} // nested types System.Type type = members[0] as System.Type; if (type != null) { // see section 10.7.1 of ECMA if (type.IsGenericTypeDefinition) type = type.MakeGenericType(clrtype.GetGenericArguments()); return new RubyMethod( new NestedTypeGetter(Load(type, null, false)), 0, Access.Public, this); } return null; }