private static int GetFieldIndexByName(this IReflect type, string fieldName) { for (var i = 0; i < type.GetFields(ReflectionFlags).Length; i++) { if (type.GetFields(ReflectionFlags)[i].Name == fieldName) { return(i); } } return(-1); }
private static void reflectionAppend(object obj, IReflect type, HashCodeBuilder builder, bool useTransients, IEnumerable <string> excludedFields) { if (REGISTRY.Value.Contains(new IDKey(obj))) { return; } try { REGISTRY.Value.Add(new IDKey(obj)); var values = type .GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) .Where(field => excludedFields == null || !excludedFields.Contains(field.Name)) .Where(field => useTransients || !field.IsNotSerialized) .Select(field => field.GetValue(obj)); foreach (var value in values) { builder.append(value); } } finally { REGISTRY.Value.Remove(new IDKey(obj)); } }
private static IEnumerable <object> GetEnumValues(IReflect type) { return (type .GetFields(BindingFlags.Public | BindingFlags.Static) .Select(i => i.GetRawConstantValue())); }
private void FillPropertiesRecursively(object instance, IReflect type, int index) { var propertyInfos = type .GetProperties(BindingFlags.Public | BindingFlags.Instance) .Select(propertyInfo => new PropertyTargetInfo(propertyInfo)); var fieldInfos = type .GetFields(BindingFlags.Public | BindingFlags.Instance) .Select(fieldInfo => new FieldTargetInfo(fieldInfo)); var targetInfos = propertyInfos.Cast <TargetInfo>().Union(fieldInfos); foreach (var targetInfo in targetInfos) { if (targetInfo.IsNullable() && !Options.FillNullables) { continue; } var buildFunction = GetBuildFunction(targetInfo, index); if (buildFunction == null) { continue; } var propertyValue = buildFunction.Invoke(); targetInfo.SetValue(instance, propertyValue); if (CanApplyRecursion(targetInfo)) { FillPropertiesRecursively(propertyValue, targetInfo.TargetType, index); } } }
private static void CopyFields( object originalObject, IDictionary <object, object> visited, object cloneObject, IReflect typeToReflect, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy, Func <FieldInfo, bool>?filter = null) { foreach (FieldInfo fieldInfo in typeToReflect.GetFields(bindingFlags)) { if (filter != null && !filter(fieldInfo)) { continue; } if (IsPrimitive(fieldInfo.FieldType)) { continue; } var originalFieldValue = fieldInfo.GetValue(originalObject); var clonedFieldValue = DeepClone_Internal(originalFieldValue, visited); fieldInfo.SetValue(cloneObject, clonedFieldValue); } }
// Function to query column values and return a string to reference changes internal string GetValues(T values, IReflect model) { var stringToSave = string.Empty; // Specifies flags that control binding and the manner in which search for types and members is performed by reflection. var bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public; // Query the fields available in the reflection model var fieldNames = model .GetFields(bindingFlags) .Select(field => field.Name) .ToList(); foreach (var field in fieldNames) { // Selected columns need a treatment to have their name exactly as it is saved in the database var start = field.IndexOf("<", StringComparison.Ordinal); var end = field.IndexOf(">", StringComparison.Ordinal); // Get column name var nameField = field.Substring(start, end - start).Replace("<", "").Replace(">", ""); // Get value column name var value = values.GetType().GetProperty(nameField)?.GetValue(values, null); // This is the return you will save in the database for future reference. stringToSave += $"[Column]: {nameField}, [Value]: {value}" + Environment.NewLine; } return(stringToSave); }
private static Dictionary <MemberInfo, object> GetMembers(IReflect type, object obj) { var members = new Dictionary <MemberInfo, object>(); const BindingFlags bindingFlagsAll = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static; var fields = type.GetFields(bindingFlagsAll); foreach (var field in fields) { members.Add(field, field.GetValue(obj)); } var properties = type.GetProperties(bindingFlagsAll); foreach (var property in properties) { var backingField = property.GetBackingField(); if (backingField != null) { members.Remove(backingField); members.Add(property, property.GetValue(obj, null)); } } return(members); }
/// <summary> /// Add the fields of an object /// </summary> private static void AddTypeFields(IReflect type, Attribute[] attributes, PropertyDescriptorCollection fields, ICollection <string> addedMemberNames) { if ((type == typeof(ArrayList)) || (type == typeof(Hashtable)) || (type == typeof(SortedList))) { return; } // get all instance FieldInfos var fieldInfos = type.GetFields(_bindingFlags); var filtering = attributes != null && attributes.Length > 0; foreach (var field in fieldInfos.Where(field => !addedMemberNames.Contains(field.Name))) { // this one made it in the list... addedMemberNames.Add(field.Name); var fieldDesc = new FieldPropertyDescriptor(field); if (!filtering || fieldDesc.Attributes.Contains(attributes)) { fields.Add(fieldDesc); } } }
private void ResolveReference(IReflect instanceType, object instance) { var fields = instanceType.GetFields( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic ); var referencedFields = fields .Where(fieldInfo => Attribute.IsDefined(fieldInfo, typeof(ReferenceAttribute))); foreach (var info in referencedFields) { var referenceName = info.FieldType.Name; var referenceInstance = FindInstance(referenceName) ?? throw new Exception(); info.SetValue(instance, referenceInstance); } var properties = instanceType.GetProperties( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic ); var referencedProperties = properties .Where(fieldInfo => Attribute.IsDefined(fieldInfo, typeof(ReferenceAttribute))); foreach (var info in referencedProperties) { var referenceName = info.PropertyType.Name; var referenceInstance = FindInstance(referenceName) ?? throw new Exception(); info.SetValue(instance, referenceInstance); } }
/// <summary> /// Get the values of type T of all constants in this class. /// </summary> /// <returns>Returns the values of type T of all constants in this class.</returns> private static IEnumerable <T> GetAllValues <T>(IReflect type) { return(type .GetFields(BindingFlags.Public | BindingFlags.Static) .Where(fi => fi.IsLiteral && !fi.IsInitOnly && fi.FieldType == typeof(T)) .Select(x => (T)x.GetRawConstantValue()) .ToList()); }
private static IEnumerable <string> GetFieldValues(IReflect targetType) { var fields = targetType.GetFields(BindingFlags.Public | BindingFlags.Static); var constants = fields.Where(fi => fi.IsLiteral && !fi.IsInitOnly); var values = constants.Select(f => f.GetValue(null)?.ToString()).ToArray(); return(values); }
/// <summary> /// Initializes a new instance of the <see cref="DefaultEnvironmentProvider"/> class. /// </summary> /// <param name="supportedType">A <see cref="IReflect"/> of a object with the supported Environment Aliases on.</param> protected DefaultEnvironmentProvider(IReflect supportedType) { this.SupportedAliases = supportedType .GetFields(BindingFlags.Static | BindingFlags.Public) .Where(f => f.IsLiteral) .Select(a => a.GetValue(supportedType) as string) .ToArray(); }
private static void PopulateCommonProperties(IReflect type, ElasticEpiDocument source, TK destination) { var fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (var fi in fields) { fi.SetValue(destination, fi.GetValue(source)); } }
public static string[] GetStaticValues(IReflect type, string exclude = null) { return(type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) .Where(fi => fi.IsLiteral && !fi.IsInitOnly) .Select(fi => (string)fi.GetValue(null)) .Where(f => exclude is null || exclude != f) .ToArray()); }
private FieldInfo FieldInfo(IReflect type) { FieldInfo fieldInfo = type .GetFields(BindingFlags.NonPublic | BindingFlags.Instance) .FirstOrDefault(t => NameMatches(t.Name)); fieldInfo.Should().NotBeNull($"expected variable [name = {_name}]"); return(fieldInfo); }
private static IEnumerable <MemberInfo> GetPublicMembers(IReflect type) { var flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static; var properties = type.GetProperties(flags); var fields = type.GetFields(flags); var members = new MemberInfo[properties.Length + fields.Length]; properties.CopyTo(members, 0); fields.CopyTo(members, properties.Length); return(members); }
/// <summary> /// Get the names of all constants in this class. /// </summary> /// <returns>Returns the names of all constants in this class.</returns> private static IEnumerable <string> GetAllNames(IReflect type) { const BindingFlags flags = BindingFlags.Static | BindingFlags.Public; var fields = type.GetFields(flags).Where(f => f.IsLiteral); foreach (var fieldInfo in fields) { yield return(fieldInfo.GetValue(null)?.ToString()); } }
private static IEnumerable <MetadataItem <string> > GetConstValues(IReflect classType) { return(classType .GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) .Where(fi => fi.IsLiteral && !fi.IsInitOnly) .Select(f => new MetadataItem <string> { Id = (string)f.GetValue(null), Name = Regex.Replace(f.Name, "[A-Z]", " $0").Trim() })); }
private static void SetPublicFields(IReflect type, object obj, Dictionary <Type, object> createdObjectReferences) { var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance); var objectGenerator = new ObjectGenerator(); foreach (var field in fields) { var fieldValue = objectGenerator.GenerateObject(field.FieldType, createdObjectReferences); field.SetValue(obj, fieldValue); } }
private static void AddFlags(ICollection <Flag> flags, IReflect T) { var fields = T.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public); foreach (var field in fields) { var flagattr = field.GetCustomAttribute <CLIFlagAttribute>(true); if (flagattr == null || flagattr.AllPositionals || flagattr.Hidden) { continue; } var flagJson = new Flag { Name = flagattr.Flag, HelpText = flagattr.Help, Choices = flagattr.Valid, IsPositional = flagattr.Positional != -1, Position = flagattr.Positional, Required = flagattr.Required, TakesValue = flagattr.NeedsValue, ValueType = FlagValueType.String }; if (flagattr.Parser != null) { var t = flagattr.Parser[1]; switch (t) { case "CLIFlagBoolean": flagJson.ValueType = FlagValueType.Boolean; break; case "CLIFlagInt": flagJson.ValueType = FlagValueType.Int; break; case "CLIFlagByte": flagJson.ValueType = FlagValueType.Byte; break; case "CLIFlagChar": flagJson.ValueType = FlagValueType.Char; break; default: throw new Exception($"UtilCommands: unable to convert parser \"{t}\" to enum"); } } flags.Add(flagJson); } }
private void ProcessFields(IReflect type, IDictionary <int, MemberMetadata> result) { foreach (var field in type.GetFields(AVAILABLE_BINDING_FLAGS).Where(new MemberSpecification())) { var index = GetMemberOffset(field); Guard.AgainstIsTrue <MetadataBuilderException>(result.ContainsKey(index), string.Format(DUBLICATED_OFFSET_ON_MEMBER_MESSAGE_PATTERN, field.Name)); var memberInfo = (IMemberInfo)Activator.CreateInstance( typeof(DeclarativeFieldInfoMember <,>).MakeGenericType(new[] { this._type, field.FieldType }), new object[] { field }); result.Add(index, this.CreateMemberMetadata(memberInfo, IsSkipped(field))); } }
private void injectMembers(IReflect type, object instance) { var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(x => x.CanWrite); foreach (var propertyInfo in properties) { propertyInfo.SetValue(instance, _container.Resolve(propertyInfo.PropertyType), null); } var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance); foreach (var fieldsInfo in fields) { fieldsInfo.SetValue(instance, _container.Resolve(fieldsInfo.FieldType)); } }
private static FieldInfo GetStateField(IReflect type) { FieldInfo stateField = null; foreach (FieldInfo field in type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)) { if (field.Name.Contains("state")) { stateField = field; } } Validate.NotNull(stateField); return(stateField); }
private IExecutable InjectDependencies(IReflect commandType, IEnumerable data, Type type) { var fieldsToInject = commandType .GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .Where(f => f.GetCustomAttributes <InjectAttribute>() != null) .ToArray(); var injectArgs = fieldsToInject .Select(f => this.serviceProvider.GetService(f.FieldType)).ToArray(); var constrArgs = new object[] { data }.Concat(injectArgs).ToArray(); var instance = (IExecutable)Activator.CreateInstance(type, constrArgs); return(instance); }
private static IEnumerable <string> GetConstants(IReflect myType, BindingFlags flags) { var fields = myType.GetFields(flags); foreach (var fieldInfo in fields) { var type = ToPrettyString(fieldInfo.FieldType); if (!returnTypeDictionary.ContainsKey(type)) { returnTypeDictionary[type] = new List <string>(); } returnTypeDictionary[type].Add(fieldInfo.Name); } return(fields.Select(x => x.Name).Distinct()); }
/// <summary>Finds and returns peristent fields of the requested group.</summary> private static IEnumerable <FieldInfo> FindAnnotatedFields( IReflect type, bool needStatic, bool needInstance, string group = null) { var flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy; if (needStatic) { flags |= BindingFlags.Static; } if (needInstance) { flags |= BindingFlags.Instance; } return(type.GetFields(flags).Where(f => FieldFilter(f, group))); }
private static IEnumerable <MemberInfo> EnumerateMembers(IReflect type) { FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public); foreach (FieldInfo fieldInfo in fields) { yield return(fieldInfo); } IEnumerable <PropertyInfo> properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public) .Where(x => x.CanRead); foreach (PropertyInfo propertyInfo in properties) { yield return(propertyInfo); } }
private static void ProcessProperties(IReflect type, IDictionary <string, ValueAccessor> valueAccessors) { var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var prop in properties) { foreach (var functionAttribute in prop.GetCustomAttributes <MoPropertyAttribute>()) { if (valueAccessors.ContainsKey(functionAttribute.Name)) { continue; } var accessor = new PropertyAccessor(prop); if (prop.GetCustomAttribute <MoObservableAttribute>() != null) { accessor.Observable = true; } valueAccessors.Add(functionAttribute.Name, accessor); } } var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance); foreach (var prop in fields) { foreach (var functionAttribute in prop.GetCustomAttributes <MoPropertyAttribute>()) { if (valueAccessors.ContainsKey(functionAttribute.Name)) { continue; } var accessor = new FieldAccessor(prop); if (prop.GetCustomAttribute <MoObservableAttribute>() != null) { accessor.Observable = true; } valueAccessors.Add(functionAttribute.Name, accessor); } } }
private static void ReflectionAppend(object obj, IReflect clazz, HashCodeBuilder builder, bool useTransients) { var fields = clazz.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField); foreach (var f in fields) { if ((f.Name.IndexOf('$') != -1) || (!useTransients && IsTransient(f)) || f.IsStatic) { continue; } try { builder.Append(f.GetValue(obj)); } catch (Exception) { throw new Exception("Unexpected IllegalAccessException"); } } }
public static DynamicFieldInfo[] GetExpandoObjectFields(object o, bool hideNamespaces) { IReflect reflect = o as IReflect; if (reflect == null) { return(new DynamicFieldInfo[0]); } try { FieldInfo[] fields = reflect.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); ArrayList list = new ArrayList(); foreach (FieldInfo info in fields) { bool flag = false; foreach (object obj2 in list) { if (info.Name == ((DynamicFieldInfo)obj2).name) { flag = true; } } if (!flag) { object obj3 = info.GetValue(o); if (!hideNamespaces || !(obj3 is Namespace)) { list.Add(new DynamicFieldInfo(info.Name, obj3, info.FieldType.Name)); } } } return((DynamicFieldInfo[])list.ToArray(typeof(DynamicFieldInfo))); } catch { return(new DynamicFieldInfo[0]); } }
private static void CopyFields( object originalObject, IDictionary <object, object> visited, object cloneObject, IReflect typeToReflect, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy, Func <FieldInfo, bool> filter = null) { var validFields = typeToReflect.GetFields(bindingFlags).Where(x => x.IsValidField()).ToList(); foreach (FieldInfo fieldInfo in validFields) { if (filter != null && filter(fieldInfo) == false) { continue; } if (IsPrimitive(fieldInfo.FieldType)) { continue; } var originalFieldValue = fieldInfo.GetValue(originalObject); var clonedFieldValue = InternalCopy(originalFieldValue, visited); fieldInfo.SetValue(cloneObject, clonedFieldValue); } }
private static void SetPublicFields(IReflect type, object obj, Dictionary<Type, object> createdObjectReferences) { var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance); var objectGenerator = new ObjectGenerator(); foreach (var field in fields) { var fieldValue = objectGenerator.GenerateObject(field.FieldType, createdObjectReferences); field.SetValue(obj, fieldValue); } }
private FieldInfoCache(IReflect type) { Fields = type.GetFields(SimpleReflection.BindingFlags); _fields = Fields.ToDictionary(x => x.Name, StringComparer.Ordinal); }
private static IEnumerable<FieldInfo> GetFields(IReflect type) { return type.GetFields(Flags); }
private IEnumerable<MemberInfo> GetFields(IReflect type) { return type .GetFields(DefaultMembersSearchFlags) .Where(x => !x.IsDefined(typeof(NonSerializedAttribute), false)); }
public static IEnumerable<FieldInfo> FindLocalizationFields(IReflect type) { return from field in type.GetFields(BindingFlags.Static | BindingFlags.Public) where GetLocalizationAttribute(field) != null select field; }
private static IEnumerable<string> GetConstants(IReflect myType, BindingFlags flags) { var fields = myType.GetFields(flags); foreach (var fieldInfo in fields) { var type = ToPrettyString(fieldInfo.FieldType); if (!returnTypeDictionary.ContainsKey(type)) returnTypeDictionary[type] = new List<string>(); returnTypeDictionary[type].Add(fieldInfo.Name); } return fields.Select(x => x.Name).Distinct(); }