Example #1
0
        public PropertyInfoEx(PropertyInfo property, bool enableDelegateCaching = true)
        {
            this.PropertyInfo = property;

            if (property.PropertyType != typeof(string))
            {
                this.EnumerableDescriptor = EnumerableTypeDescriptor.Get(property.PropertyType);
                this.IsEnumerable         = this.EnumerableDescriptor.IsEnumerable;
            }

            this.IndexerCount = PropertyInfo.GetIndexParameters().Count();

            //PrimitiveLike?

            var backingField = property.DeclaringType.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)
                               .Where(x => x.HasAttribute <CompilerGeneratedAttribute>() &&
                                      (x.Name == "<" + property.Name + ">k__BackingField" || // Auto property
                                       x.Name == "<" + property.Name + ">i__Field")) //Anonymous type
                               .FirstOrDefault();

            if (backingField != null)
            {
                BackingFieldName = backingField.Name;
            }

            InitializeGet(enableDelegateCaching);
            InitializeSet(enableDelegateCaching);
        }
Example #2
0
        public FieldInfoEx(FieldInfo field, bool enableDelegateCaching = true)
        {
            this.FieldInfo = field;

            if (field.FieldType != typeof(string))
            {
                this.EnumerableDescriptor = EnumerableTypeDescriptor.Get(field.FieldType);
                this.IsEnumerable         = this.EnumerableDescriptor.IsEnumerable;
            }

            if (FieldInfo.IsInitOnly && FieldInfo.Name.StartsWith("<") && FieldInfo.Name.EndsWith(">i__Field"))
            {
                AutoPropertyName = FieldInfo.Name.Substring(1, FieldInfo.Name.Length - 10);
            }
            if (FieldInfo.HasAttribute <CompilerGeneratedAttribute>() &&
                FieldInfo.Name.StartsWith("<") && FieldInfo.Name.EndsWith(">k__BackingField"))
            {
                AutoPropertyName = FieldInfo.Name.Substring(1, FieldInfo.Name.Length - 17);
            }

            //PrimitiveLike?

            if (enableDelegateCaching)
            {
                this.Getter = MemberAccessorDelegateBuilder.CachedFieldBuilder.BuildGenericGetter(this.FieldInfo);
                this.Setter = MemberAccessorDelegateBuilder.CachedFieldBuilder.BuildGenericSetter(this.FieldInfo);
            }
            else
            {
                var builder = new FieldAccessorLambdaBuilder(false);
                this.Getter = builder.BuildGenericGetter(this.FieldInfo).Compile();
                this.Setter = builder.BuildGenericSetter(this.FieldInfo).Compile();
            }
        }
Example #3
0
        public static EnumerableTypeDescriptor Get(Type type)
        {
            if (!descriptorCache.TryGetValue(type, out var enumerableTypeDescriptor))
            {
                enumerableTypeDescriptor = new EnumerableTypeDescriptor(type);
                descriptorCache.TryAdd(type, enumerableTypeDescriptor);
            }

            return(enumerableTypeDescriptor);
        }
Example #4
0
 public static bool IsEnumerable(this Type type, out EnumerableTypeDescriptor enumerableTypeDescriptor)
 {
     enumerableTypeDescriptor = EnumerableTypeDescriptor.Get(type);
     return(enumerableTypeDescriptor.IsEnumerable);
 }