/// <summary> /// Creates the equivalent <see cref="BindingFlags"/> by the <see cref="MemberDescriptor"/>'s /// MemberScope, MemberAccessibility and CaseSensitivity configuration for use in /// <see cref="System.Reflection"/> calls. The <see cref="BindingFlags"/> instance is only resolved /// once and cached in the <see cref="Nullable{BindingFlags}"/> field <see cref="_bindingFlags"/>. /// </summary> /// <returns> /// The equivalent <see cref="BindingFlags"/> configuration for this <see cref="MemberDescriptor"/> /// </returns>lol private BindingFlags resolveBindingFlags() { if (_bindingFlags.HasValue) { return(_bindingFlags.Value); } var bindingFlags = (BindingFlags.Default | (BindingFlags)MemberAccessibility | (BindingFlags)MemberScope); if (CaseInsensitive) { bindingFlags |= BindingFlags.IgnoreCase; } return((_bindingFlags = bindingFlags).Value); }
protected PropsModel(Type type, object instance, BindingFlags?flags) { Type = type; DisplayName = type.GetCustomAttribute <DisplayNameAttribute>()? .DisplayName ?? type.Name; if (flags == null) { flags = instance == null ? BindingFlags.Public | BindingFlags.Static : BindingFlags.Public | BindingFlags.Instance; } PropertyInfo[] props = type.GetProperties(flags.Value); //var models = props.Select((prop) => new PropertyModel(prop)); var models = from prop in props select new PropModel(prop, instance); Properties = new ReadOnlyCollection <PropModel>(models.ToList()); }
/// <summary> /// Gets the original properties from declared types whenever they are found by derived types. /// </summary> /// <param name="type">The type.</param> /// <param name="flags">The flags.</param> /// <returns></returns> public static PropertyInfo[] GetOriginalProperties(this Type type, BindingFlags?flags = null) { BindingFlags fl = flags.HasValue ? flags.Value : (BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance); IEnumerable <PropertyInfo> properties = type.GetProperties(fl); return(properties.Select(info => { if (info.DeclaringType == null || info.DeclaringType == info.ReflectedType) { return info; } return info.DeclaringType.GetProperty(info.Name, fl); } ) .ToArray() ); }
public static object Invoke(object obj, string methodName, Type type, Type[] typeParams, BindingFlags? flags = null, params object[] parameters) { if (string.IsNullOrEmpty(methodName)) return null; if (type == null) type = obj is Type ? (Type) obj : obj.GetType(); var f = type.GetMethod(methodName, flags ?? AllFlags, null, CallingConventions.Standard, typeParams, null); if (f != null) return f.Invoke(obj, parameters); Debug.LogWarning(string.Format("Invoke Error : <{0}> is not a method of type <{1}>", methodName, type)); return null; /* ArgumentException: failed to convert parameters System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:192) System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115) vietlabs.h2.h2_Reflection.Invoke (System.Object obj, System.String methodName, System.Type type, Nullable`1 flags, System.Object[] parameters) (at Assets/Plugins/Editor/Hierarchy2/Editor/Scripts/core/h2_Reflection.cs:41) vietlabs.h2.h2_Camera.SetAnimT[Single] (System.String name, Single value) (at Assets/Plugins/Editor/Hierarchy2/Editor/Scripts/h2/features/h2_Common.cs:368) vietlabs.h2.h2_Camera.set_orthographic (Boolean value) (at Assets/Plugins/Editor/Hierarchy2/Editor/Scripts/h2/features/h2_Common.cs:294) vietlabs.h2.h2_Camera.CopyFrom (UnityEngine.Camera cam) (at Assets/Plugins/Editor/Hierarchy2/Editor/Scripts/h2/features/h2_Common.cs:338) vietlabs.h2.h2_Camera.LookThrough (UnityEngine.Camera cam) (at Assets/Plugins/Editor/Hierarchy2/Editor/Scripts/h2/features/h2_Common.cs:416) vietlabs.h2.h2_Common.RunCommand (System.String cmd) (at Assets/Plugins/Editor/Hierarchy2/Editor/Scripts/h2/features/h2_Common.cs:56) vietlabs.h2.h2_MatchData.Trigger (System.Collections.Generic.List`1 matchList) (at Assets/Plugins/Editor/Hierarchy2/Editor/Scripts/core/h2_Shortcut.cs:201) vietlabs.h2.h2_MatchData.Check (UnityEngine.Event e, System.Collections.Generic.Dictionary`2 hndMap) (at Assets/Plugins/Editor/Hierarchy2/Editor/Scripts/core/h2_Shortcut.cs:169) vietlabs.h2.h2_Shortcut.Check () (at Assets/Plugins/Editor/Hierarchy2/Editor/Scripts/core/h2_Shortcut.cs:84) vietlabs.h2.Hierarchy2.HierarchyItemCB (Int32 instID, Rect r) (at Assets/Plugins/Editor/Hierarchy2/Editor/Scripts/h2/Hierarchy2.cs:222) System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222) Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation. System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232) System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115) System.Delegate.DynamicInvokeImpl (System.Object[] args) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Delegate.cs:443) System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/MulticastDelegate.cs:71) System.Delegate.DynamicInvoke (System.Object[] args) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Delegate.cs:415) FavoritesTab+TreeViewTracker.HierarchyItemOnGuiCallback (Int32 item, Rect selectionRect) (at Assets/FlipbookGames/FavoritesTab/Editor/Scripts/FavoritesTab.cs:1754) UnityEditor.SceneHierarchyWindow.OnGUIAssetCallback (Int32 instanceID, Rect rect) (at C:/buildslave/unity/build/Editor/Mono/SceneHierarchyWindow.cs:230) UnityEditor.TreeView.OnGUI (Rect rect, Int32 keyboardControlID) (at C:/buildslave/unity/build/Editor/Mono/GUI/TreeView/TreeView.cs:404) UnityEditor.SceneHierarchyWindow.DoTreeView (Single searchPathHeight) (at C:/buildslave/unity/build/Editor/Mono/SceneHierarchyWindow.cs:334) UnityEditor.SceneHierarchyWindow.OnGUI () (at C:/buildslave/unity/build/Editor/Mono/SceneHierarchyWindow.cs:178) System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222) */ }
/// <summary> /// Get the value of the given field or property name and <see cref="BindingFlags"/>. /// </summary> /// <param name="name">The name of the field or property.</param> /// <param name="flags">The <see cref="BindingFlags"/> to use.</param> public Optional <TResult> GetSafe <TResult>(string name, BindingFlags?flags = null) { var field = Optional.Of(typeof(T).GetField(name, flags ?? Bindings)); var prop = Optional.Of(typeof(T).GetProperty(name, flags ?? Bindings)); if (_throwIfNullMember) { switch (field.HasValue) { case false when !prop.HasValue: throw new ValueException( $"Neither a field or property \"{name}\" exists on type {typeof(T).AsPrettyString()} with the given BindingFlags."); case true when prop.HasValue: throw new ValueException("??????????????????????????????? how"); //whatexception has returned } } var spine = BackingObject; //lambda scopes MemberInfo memberInfo = null; if (field.HasValue) { memberInfo = field.Value; } else if (prop.HasValue) { memberInfo = prop.Value; } var opt = Optional.Of(memberInfo); return(opt.HasValue ? opt.Value is FieldInfo fi ? fi.GetValue(spine).Cast <TResult>() : opt.Value is PropertyInfo pi ? pi.GetValue(spine).Cast <TResult>() : Optional.None <TResult>() : Optional.None <TResult>()); }
public static object Invoke(object obj, string methodName, Type type = null, BindingFlags?flags = null, params object[] parameters) { if (string.IsNullOrEmpty(methodName)) { return(null); } if (type == null) { type = obj is Type ? (Type)obj : obj.GetType(); } var f = type.GetMethod(methodName, flags ?? AllFlags); if (f != null) { return(f.Invoke(obj, parameters)); } Debug.LogWarning(string.Format("Invoke Error : <{0}> is not a method of type <{1}>", methodName, type)); return(null); }
public static object xGetField(this object obj, string name, Type type = null, BindingFlags?flags = null) { if (obj == null || string.IsNullOrEmpty(name)) { return(false); } if (type == null) { type = (obj is Type) ? (Type)obj : obj.GetType(); } var field = type.GetField(name, flags ?? AllFlags); if (field == null) { Debug.LogWarning( string.Format("GetField Error : <{0}> does not contains a field with name <{1}>", type, name)); return(null); } return(field.GetValue(obj)); }
public static object GetProperty(object obj, string name, Type type = null, BindingFlags?flags = null) { if (obj == null || string.IsNullOrEmpty(name)) { return(null); } if (type == null) { type = obj is Type ? (Type)obj : obj.GetType(); } var property = type.GetProperty(name, flags ?? AllFlags); if (property != null) { return(property.GetValue(obj, null)); } Debug.LogWarning( string.Format("GetProperty Error : <{0}> does not contains a property with name <{1}>", type, name)); return(null); }
public static void xSetProperty(this object obj, string name, object value, Type type = null, BindingFlags?flags = null) { if (obj == null || string.IsNullOrEmpty(name)) { return; } if (type == null) { type = (obj is Type) ? (Type)obj : obj.GetType(); } var property = type.GetProperty(name, flags ?? AllFlags); if (property == null) { Debug.LogWarning( string.Format("SetProperty Error : <{0}> does not contains a property with name <{1}>", obj, name)); return; } property.SetValue(obj, value, null); }
private static PropertyInfo[] GetProps <T>(T model, BindingFlags?bindingAttr) { var key = model.GetType().FullName; if (_cachedPropsList.ContainsKey(key)) { return(_cachedPropsList[key]); } else { var props = typeof(T). GetProperties(bindingAttr ?? (BindingFlags.Public | BindingFlags.Instance)); lock (_root) { if (!_cachedPropsList.ContainsKey(key)) { _cachedPropsList.Add(key, props); } } return(props); } }
public static Func <T> RequireConstructor <T>( Type concreteType = null, BindingFlags?bindingFlags = default) { // var typeOfT = typeof(T); concreteType = concreteType .Arg(nameof(concreteType)) .EnsureCompatible(typeOfT) .Value ?? typeOfT; // ConstructorInfo constructor; P_EnsureInstantiationAbility( concreteType, bindingFlags ?? FindConstructorDefaultBindingFlags, out constructor); // return(P_CreateConstructorDelegate <Func <T> >(baseType: typeOfT, concreteType: concreteType, argsBaseTypes: null, argsConcreteTypes: null, bindingFlags: bindingFlags)); }
internal static MethodInfo GetGenericMethodByArgumentCountInternal( Type declaringType, string methodName, int numberOfTypeArguments, int numberOfArguments, Func <ParameterInfo[], bool> predicate, BindingFlags?bindingFlags) { predicate = predicate ?? (p => true); MethodInfo[] methodInfos = bindingFlags.HasValue ? declaringType.GetMethods(bindingFlags.Value) : declaringType.GetMethods(); var query = from m in methodInfos let match = m.Name == methodName && m.GetGenericArguments().Length == numberOfTypeArguments && m.GetParameters().Length == numberOfArguments && predicate(m.GetParameters()) where match select m; return(query.Single()); }
// TODO: Реализовать кэширование делегатов конструкторов (если имеет смысл в плане производительности). // static TDelegate P_CreateConstructorDelegate <TDelegate>( Type baseType, Type concreteType, Type[] argsBaseTypes, Type[] argsConcreteTypes, BindingFlags?bindingFlags = default) { // ConstructorInfo constructor; P_EnsureInstantiationAbility(type: concreteType, bindingFlags: bindingFlags ?? FindConstructorDefaultBindingFlags, foundConstructor: out constructor, argsConcreteTypes: argsConcreteTypes); var constructorDeclaredParameters = constructor .GetParameters(); var argsBaseTypeExprs = argsBaseTypes.EmptyIfNull() .Select((locArgType, locIndex) => Expression.Parameter(locArgType, string.Format(CultureInfo.InvariantCulture, "arg{0}", locIndex))) .ToArray(); var argsConcreteTypeExprs = argsBaseTypeExprs .Select( selector: (locArgBaseTypeExpr, locIndex) => locArgBaseTypeExpr.Type == argsConcreteTypes[locIndex] ? (Expression)locArgBaseTypeExpr : Expression.Convert(locArgBaseTypeExpr, argsConcreteTypes[locIndex])) .Select( selector: (locArgConcreteTypeExpr, locIndex) => locArgConcreteTypeExpr.Type == constructorDeclaredParameters[locIndex].ParameterType ? locArgConcreteTypeExpr : Expression.Convert(locArgConcreteTypeExpr, constructorDeclaredParameters[locIndex].ParameterType)) .ToArray(); return ((baseType == concreteType ? Expression.Lambda <TDelegate>( body: Expression.New(constructor, argsConcreteTypeExprs), parameters: argsBaseTypeExprs) : Expression.Lambda <TDelegate>( body: Expression.Convert(Expression.New(constructor, argsConcreteTypeExprs), baseType), parameters: argsBaseTypeExprs)) .Compile()); }
/// <summary>Gets the properties.</summary> /// <param name="type">The type.</param> /// <param name="flags">The flags.</param> /// <returns></returns> public virtual PropertyInfo[] GetProperties(Type type, BindingFlags?flags = null) { if (type.IsInterface) { var propertyInfos = new List <PropertyInfo>(); var considered = new List <Type>(); var queue = new Queue <Type>(); considered.Add(type); queue.Enqueue(type); while (queue.Count > 0) { var subType = queue.Dequeue(); foreach (var subInterface in subType.GetInterfaces()) { if (considered.Contains(subInterface)) { continue; } considered.Add(subInterface); queue.Enqueue(subInterface); } var typeProperties = flags.HasValue ? subType.GetProperties(flags.Value) : subType.GetProperties(); var newPropertyInfos = typeProperties .Where(x => !propertyInfos.Contains(x)); propertyInfos.InsertRange(0, newPropertyInfos); } return(propertyInfos.ToArray()); } return(flags.HasValue ? type.GetProperties(flags.Value) : type.GetProperties()); }
/// <summary> Public属性の関数を実行 </summary> public static object InvokePublicMethod <T>(T instance, string methodName, object[] parameters = null, BindingFlags?bindingFlags = null) { var flags = BindingFlags.Public | BindingFlags.Instance; if (bindingFlags.HasValue) { flags |= bindingFlags.Value; } var methodInfo = GetMethodInfo(typeof(T), methodName, flags); return(methodInfo.Invoke(instance, parameters)); }
/// <summary> Private属性のプロパティに値を設定 </summary> public static void SetPrivateProperty <T, TValue>(T instance, string propertyName, TValue value, object[] index = null, BindingFlags?bindingFlags = null) { var flags = BindingFlags.GetProperty | BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Instance; if (bindingFlags.HasValue) { flags |= bindingFlags.Value; } var propertyInfo = GetPropertyInfo(typeof(T), propertyName, flags); if (propertyInfo.CanWrite) { propertyInfo.SetValue(instance, value, index); } else { throw new InvalidOperationException("This property can not be written"); } }
/// <summary>Gets the properties with values.</summary> /// <param name="data">The data.</param> /// <param name="flags">The flags.</param> /// <returns></returns> public virtual Dictionary <string, object> GetPropertiesWithValues(object data, BindingFlags?flags = null) { if (data == null) { return(new Dictionary <string, object>()); } var props = GetProperties(data.GetType(), flags); return(props .Where(x => x.CanRead) .ToDictionary(x => x.Name, x => x.GetValue(data))); }
public static IEnumerable <TypeDefinition> GetNestedTypesOnType(this TypeDefinition type, Func <TypeDefinition, bool> filter, BindingFlags?bindingFlags = BindingFlags.Default) { foreach (var nestedType in type.NestedTypes) { if (filter != null && !filter(nestedType)) { continue; } if ((bindingFlags & (BindingFlags.Public | BindingFlags.NonPublic)) == BindingFlags.Public) { if (!nestedType.IsNestedPublic) { continue; } } if ((bindingFlags & (BindingFlags.Public | BindingFlags.NonPublic)) == BindingFlags.NonPublic) { if (nestedType.IsNestedPublic) { continue; } } yield return(nestedType); } }
public static IEnumerable <EventDefinition> GetEventsOnTypeHierarchy(this TypeDefinition type, Func <EventDefinition, bool> filter, BindingFlags?bindingFlags = BindingFlags.Default) { bool onBaseType = false; while (type != null) { foreach (var @event in type.Events) { // Ignore private properties on a base type - those are completely ignored by reflection // (anything private on the base type is not visible via the derived type) // Note that properties themselves are not actually private, their accessors are if (onBaseType && (@event.AddMethod == null || @event.AddMethod.IsPrivate) && (@event.RemoveMethod == null || @event.RemoveMethod.IsPrivate)) { continue; } if (filter != null && !filter(@event)) { continue; } if ((bindingFlags & (BindingFlags.Instance | BindingFlags.Static)) == BindingFlags.Static) { if ((@event.AddMethod != null) && [email protected]) { continue; } if ((@event.RemoveMethod != null) && [email protected]) { continue; } } if ((bindingFlags & (BindingFlags.Instance | BindingFlags.Static)) == BindingFlags.Instance) { if ((@event.AddMethod != null) && @event.AddMethod.IsStatic) { continue; } if ((@event.RemoveMethod != null) && @event.RemoveMethod.IsStatic) { continue; } } if ((bindingFlags & (BindingFlags.Public | BindingFlags.NonPublic)) == BindingFlags.Public) { if ((@event.AddMethod == null || [email protected]) && (@event.RemoveMethod == null || [email protected])) { continue; } } if ((bindingFlags & (BindingFlags.Public | BindingFlags.NonPublic)) == BindingFlags.NonPublic) { if ((@event.AddMethod != null) && @event.AddMethod.IsPublic) { continue; } if ((@event.RemoveMethod != null) && @event.RemoveMethod.IsPublic) { continue; } } yield return(@event); } type = type.BaseType?.Resolve(); onBaseType = true; } }
public static IEnumerable <FieldDefinition> GetFieldsOnTypeHierarchy(this TypeDefinition type, Func <FieldDefinition, bool> filter, BindingFlags?bindingFlags = BindingFlags.Default) { bool onBaseType = false; while (type != null) { foreach (var field in type.Fields) { // Ignore private fields on a base type - those are completely ignored by reflection // (anything private on the base type is not visible via the derived type) if (onBaseType && field.IsPrivate) { continue; } // Note that compiler generated fields backing some properties and events will get through here. // This is intentional as reflection treats these as fields as well. if (filter != null && !filter(field)) { continue; } if ((bindingFlags & (BindingFlags.Instance | BindingFlags.Static)) == BindingFlags.Static && !field.IsStatic) { continue; } if ((bindingFlags & (BindingFlags.Instance | BindingFlags.Static)) == BindingFlags.Instance && field.IsStatic) { continue; } if ((bindingFlags & (BindingFlags.Public | BindingFlags.NonPublic)) == BindingFlags.Public && !field.IsPublic) { continue; } if ((bindingFlags & (BindingFlags.Public | BindingFlags.NonPublic)) == BindingFlags.NonPublic && field.IsPublic) { continue; } yield return(field); } type = type.BaseType?.Resolve(); onBaseType = true; } }
public static IEnumerable <MethodDefinition> GetMethodsOnTypeHierarchy(this TypeDefinition type, Func <MethodDefinition, bool> filter, BindingFlags?bindingFlags = null) { bool onBaseType = false; while (type != null) { foreach (var method in type.Methods) { // Ignore constructors as those are not considered methods from a reflection's point of view if (method.IsConstructor) { continue; } // Ignore private methods on a base type - those are completely ignored by reflection // (anything private on the base type is not visible via the derived type) if (onBaseType && method.IsPrivate) { continue; } // Note that special methods like property getter/setter, event adder/remover will still get through and will be marked. // This is intentional as reflection treats these as methods as well. if (filter != null && !filter(method)) { continue; } if ((bindingFlags & (BindingFlags.Instance | BindingFlags.Static)) == BindingFlags.Static && !method.IsStatic) { continue; } if ((bindingFlags & (BindingFlags.Instance | BindingFlags.Static)) == BindingFlags.Instance && method.IsStatic) { continue; } if ((bindingFlags & (BindingFlags.Public | BindingFlags.NonPublic)) == BindingFlags.Public && !method.IsPublic) { continue; } if ((bindingFlags & (BindingFlags.Public | BindingFlags.NonPublic)) == BindingFlags.NonPublic && method.IsPublic) { continue; } yield return(method); } type = type.BaseType?.Resolve(); onBaseType = true; } }
/// <summary> Private属性の変数の値を取得 </summary> public static TResult GetPrivateField <T, TResult>(T instance, string fieldName, BindingFlags?bindingFlags = null) { var flags = BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance; if (bindingFlags.HasValue) { flags |= bindingFlags.Value; } var fieldInfo = GetFieldInfo(typeof(T), fieldName, flags); return((TResult)fieldInfo.GetValue(instance)); }
public static MemberInfo[] GetAllMembers( BindingFlags?bindingFlags = null, Func <MemberInfo, bool> filter = null) => TypeController.GetAllMembers( typeof(T), bindingFlags, filter);
/// <summary> Public属性のプロパティの値を取得 </summary> public static TResult GetPublicProperty <T, TResult>(T instance, string propertyName, object[] index = null, BindingFlags?bindingFlags = null) { var flags = BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance; if (bindingFlags.HasValue) { flags |= bindingFlags.Value; } var propertyInfo = GetPropertyInfo(typeof(T), propertyName, flags); return((TResult)propertyInfo.GetValue(instance, index)); }
/// <summary> Private属性の変数に値を設定 </summary> public static void SetPrivateField <T, TValue>(T instance, string fieldName, TValue value, BindingFlags?bindingFlags = null) { var flags = BindingFlags.GetField | BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Instance; if (bindingFlags.HasValue) { flags |= bindingFlags.Value; } var fieldInfo = GetFieldInfo(typeof(T), fieldName, flags); fieldInfo.SetValue(instance, value); }
/// <summary> /// Create new builder for nested type /// </summary> /// <param name="type"></param> /// <param name="name"></param> /// <param name="constructorsBindings"></param> /// <returns></returns> public static TypeBuilder NewBuilderForNestedType(this Type type, string name, BindingFlags?constructorsBindings = null) { // create var assemblyName = Guid.NewGuid().ToString(); var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(assemblyName), AssemblyBuilderAccess.Run); var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName); var typeBuilder = moduleBuilder.DefineType(name, TypeAttributes.Public, type); // copy constructors var constructors = constructorsBindings.HasValue ? type.GetConstructors(constructorsBindings.Value) : type.GetConstructors(); constructors.ToList().ForEach(c => typeBuilder.AddConstructor(c)); // return return(typeBuilder); }
public static FieldInfo[] GetAllFields( BindingFlags?bindingFlags = null, Func <FieldInfo, bool> filter = null) => TypeController.GetAllFields( typeof(T), bindingFlags, filter);
public static object GetFieldValue(this Type type, string fieldName, object obj = null, BindingFlags?flags = null) { if (flags == null) { flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public; } FieldInfo field = type.GetField(fieldName, flags.Value); return(field.GetValue(obj)); }
public static MethodInfo FindMethod(this Type type, string methodName, int paramCount, BindingFlags?bindingFlags = null) { var flags = bindingFlags == null ? BindingFlagsAll : bindingFlags.Value; var ti = type; //.GetTypeInfo(); var method = ti.GetMethod(methodName, flags); if (method != null) { return(method); } var methods = ti.GetMethods(flags).Where(m => m.Name == methodName && m.GetParameters().Length == paramCount).ToArray(); Util.Check(methods.Length > 0, "Method {0} not found on type {1}.", methodName, type); Util.Check(methods.Length < 2, "Found more than one method {0} on type {1}.", methodName, type); return(methods[0]); }
public static IEnumerable <MethodDefinition> GetConstructorsOnType(this TypeDefinition type, Func <MethodDefinition, bool> filter, BindingFlags?bindingFlags = null) { foreach (var method in type.Methods) { if (!method.IsConstructor) { continue; } if (filter != null && !filter(method)) { continue; } if ((bindingFlags & (BindingFlags.Instance | BindingFlags.Static)) == BindingFlags.Static && !method.IsStatic) { continue; } if ((bindingFlags & (BindingFlags.Instance | BindingFlags.Static)) == BindingFlags.Instance && method.IsStatic) { continue; } if ((bindingFlags & (BindingFlags.Public | BindingFlags.NonPublic)) == BindingFlags.Public && !method.IsPublic) { continue; } if ((bindingFlags & (BindingFlags.Public | BindingFlags.NonPublic)) == BindingFlags.NonPublic && method.IsPublic) { continue; } yield return(method); } }