Example #1
0
        private static void CreateObjectPropertyGraphs(TypeInfo cls, ObjectGraph obj)
        {
            foreach (var p in cls.GetProperties().Where(w => w.MemberType == MemberTypes.Property))
            {
                var pg = new PropertyGraph
                {
                    Name          = _nullableRegex.IsMatch(p.Name) ? _nullableRegex.Match(p.Name).Value : p.Name,
                    SourceType    = p.PropertyType.GetFormattedName(true),
                    IsKeyProperty = p.Name.EndsWith("Id", StringComparison.CurrentCultureIgnoreCase), //NOTE: cheesy
                };

                foreach (var m in p.GetAccessors())
                {
                    if (m.Name.StartsWith("get_"))
                    {
                        pg.GetterVisibility = (m.IsPublic) ? MethodVisibilities.Public :
                                              m.IsPrivate ? MethodVisibilities.Private : MethodVisibilities.Protected;
                    }

                    if (m.Name.StartsWith("set_"))
                    {
                        pg.SetterVisibility = (m.IsPublic) ? MethodVisibilities.Public :
                                              m.IsPrivate ? MethodVisibilities.Private : MethodVisibilities.Protected;
                    }
                }

                obj.Properties.Add(pg);
                Text.GrayLine(
                    $"\tProperty: {p.Name}, Type: {p.PropertyType.GetFormattedName()}, Get: {Enum.GetName(typeof(MethodVisibilities), pg.GetterVisibility)}, Set: {Enum.GetName(typeof(MethodVisibilities), pg.SetterVisibility)}");
            }
        }
Example #2
0
        internal ClassMetadata(TypeInfo typeInfo)
        {
            #if !DataAnnotations_Missing
            var table = (TableAttribute)typeInfo.GetCustomAttributes(typeof(TableAttribute), true).SingleOrDefault();
            if (table != null)
            {
                MappedTableName = table.Name;
                MappedSchemaName = table.Schema;
            }
            #endif

            #if Weird_Reflection
            var shadowingProperties = (from p in typeInfo.DeclaredProperties where IsHidingMember(p) select p).ToList();
            var propertyList = typeInfo.DeclaredProperties.ToList();
            #elif TypeInfo_Is_Not_Type
            var type = typeInfo.AsType();
            var shadowingProperties = (from p in type.GetProperties() where IsHidingMember(p) select p).ToList();
            var propertyList = type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            #else
            var shadowingProperties = (from p in typeInfo.GetProperties() where IsHidingMember(p) select p).ToList();
            var propertyList = typeInfo.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            #endif

            Func<PropertyInfo, bool> IsHidden = propertyInfo => !shadowingProperties.Contains(propertyInfo) && shadowingProperties.Any(p => p.Name == propertyInfo.Name);

            Properties = new PropertyMetadataCollection(propertyList.Where(p => !IsHidden(p)).Select(p => new PropertyMetadata(p)));

            //List the properties that are affected when the indicated property is modified.
            foreach (var property in Properties)
                foreach (CalculatedFieldAttribute fieldList in property.PropertyInfo.GetCustomAttributes(typeof(CalculatedFieldAttribute), true))
                    foreach (var field in fieldList.SourceProperties)
                    {
                        if (!Properties.Contains(field))
                            throw new InvalidOperationException(string.Format("Cannot find property {0} on type {1}. This is needed for the calculated property {2}", field, typeInfo.FullName, property.Name));

                        Properties[field].AddCalculatedField(property);
                    }

            foreach (var property in Properties)
                property.EndInit();

            Constructors = new ConstructorMetadataCollection(typeInfo.DeclaredConstructors);
        }
Example #3
0
        } // End Function GetGetter

        // ------------------------------------



        public static System.Reflection.MemberInfo[] GetFieldsAndProperties(System.Type t)
        {
            System.Reflection.TypeInfo ti = System.Reflection.IntrospectionExtensions.GetTypeInfo(t);

            System.Reflection.FieldInfo[]    fis = ti.GetFields();
            System.Reflection.PropertyInfo[] pis = ti.GetProperties();
            System.Reflection.MemberInfo[]   mis = new System.Reflection.MemberInfo[fis.Length + pis.Length];
            System.Array.Copy(fis, mis, fis.Length);
            System.Array.Copy(pis, 0, mis, fis.Length, pis.Length);

            return(mis);
        } // End Function GetFieldsAndProperties
Example #4
0
        void SetupPart(System.Reflection.TypeInfo sourceType, BindingExpressionPart part)
        {
            part.Arguments  = null;
            part.LastGetter = null;
            part.LastSetter = null;

            PropertyInfo property = null;

            if (part.IsIndexer)
            {
                if (sourceType.IsArray)
                {
                    int index;
                    if (!int.TryParse(part.Content, NumberStyles.Number, CultureInfo.InvariantCulture, out index))
                    {
                        Console.WriteLine($"Binding : {part.Content} could not be parsed as an index for a {sourceType}");
                    }
                    else
                    {
                        part.Arguments = new object[] { index }
                    };

                    part.LastGetter = sourceType.GetDeclaredMethod("Get");
                    part.LastSetter = sourceType.GetDeclaredMethod("Set");
                    part.SetterType = sourceType.GetElementType();
                }

                DefaultMemberAttribute defaultMember = sourceType.GetCustomAttributes(typeof(DefaultMemberAttribute), true).OfType <DefaultMemberAttribute>().FirstOrDefault();
                string indexerName = defaultMember != null ? defaultMember.MemberName : "Item";

                part.IndexerName = indexerName;

#if NETSTANDARD2_0
                try
                {
                    property = sourceType.GetDeclaredProperty(indexerName);
                }
                catch (AmbiguousMatchException)
                {
                    // Get most derived instance of property
                    foreach (var p in sourceType.GetProperties().Where(prop => prop.Name == indexerName))
                    {
                        if (property == null || property.DeclaringType.IsAssignableFrom(property.DeclaringType))
                        {
                            property = p;
                        }
                    }
                }
#else
                property = sourceType.GetDeclaredProperty(indexerName);
#endif

                if (property == null) //is the indexer defined on the base class?
                {
                    property = sourceType.BaseType?.GetProperty(indexerName);
                }
                if (property == null) //is the indexer defined on implemented interface ?
                {
                    foreach (var implementedInterface in sourceType.ImplementedInterfaces)
                    {
                        property = implementedInterface.GetProperty(indexerName);
                        if (property != null)
                        {
                            break;
                        }
                    }
                }

                if (property != null)
                {
                    ParameterInfo parameter = property.GetIndexParameters().FirstOrDefault();
                    if (parameter != null)
                    {
                        try
                        {
                            object arg = Convert.ChangeType(part.Content, parameter.ParameterType, CultureInfo.InvariantCulture);
                            part.Arguments = new[] { arg };
                        }
                        catch (FormatException)
                        {
                        }
                        catch (InvalidCastException)
                        {
                        }
                        catch (OverflowException)
                        {
                        }
                    }
                }
            }
            else
            {
                property = sourceType.GetDeclaredProperty(part.Content) ?? sourceType.BaseType?.GetProperty(part.Content);
            }

            if (property != null)
            {
                if (property.CanRead && property.GetMethod != null)
                {
                    if (property.GetMethod.IsPublic && !property.GetMethod.IsStatic)
                    {
                        part.LastGetter = property.GetMethod;
                    }
                }
                if (property.CanWrite && property.SetMethod != null)
                {
                    if (property.SetMethod.IsPublic && !property.SetMethod.IsStatic)
                    {
                        part.LastSetter = property.SetMethod;
                        part.SetterType = part.LastSetter.GetParameters().Last().ParameterType;

                        if (Binding.AllowChaining)
                        {
                            FieldInfo bindablePropertyField = sourceType.GetDeclaredField(part.Content + "Property");
                            if (bindablePropertyField != null && bindablePropertyField.FieldType == typeof(BindableProperty) && sourceType.ImplementedInterfaces.Contains(typeof(IElementController)))
                            {
                                MethodInfo setValueMethod = null;
#if NETSTANDARD1_0
                                foreach (MethodInfo m in sourceType.AsType().GetRuntimeMethods())
                                {
                                    if (m.Name.EndsWith("IElementController.SetValueFromRenderer"))
                                    {
                                        ParameterInfo[] parameters = m.GetParameters();
                                        if (parameters.Length == 2 && parameters[0].ParameterType == typeof(BindableProperty))
                                        {
                                            setValueMethod = m;
                                            break;
                                        }
                                    }
                                }
#else
                                setValueMethod = typeof(IElementController).GetMethod("SetValueFromRenderer", new[] { typeof(BindableProperty), typeof(object) });
#endif
                                if (setValueMethod != null)
                                {
                                    part.LastSetter = setValueMethod;
                                    part.IsBindablePropertySetter = true;
                                    part.BindablePropertyField    = bindablePropertyField.GetValue(null);
                                }
                            }
                        }
                    }
                }
#if !NETSTANDARD1_0
                //TupleElementNamesAttribute tupleEltNames;
                //if (property != null
                //    && part.NextPart != null
                //    && property.PropertyType.IsGenericType
                //    && (property.PropertyType.GetGenericTypeDefinition() == typeof(ValueTuple<>)
                //        || property.PropertyType.GetGenericTypeDefinition() == typeof(ValueTuple<,>)
                //        || property.PropertyType.GetGenericTypeDefinition() == typeof(ValueTuple<,,>)
                //        || property.PropertyType.GetGenericTypeDefinition() == typeof(ValueTuple<,,,>)
                //        || property.PropertyType.GetGenericTypeDefinition() == typeof(ValueTuple<,,,,>)
                //        || property.PropertyType.GetGenericTypeDefinition() == typeof(ValueTuple<,,,,,>)
                //        || property.PropertyType.GetGenericTypeDefinition() == typeof(ValueTuple<,,,,,,>)
                //        || property.PropertyType.GetGenericTypeDefinition() == typeof(ValueTuple<,,,,,,,>))
                //    && (tupleEltNames = property.GetCustomAttribute(typeof(TupleElementNamesAttribute)) as TupleElementNamesAttribute) != null)
                //{
                //    // modify the nextPart to access the tuple item via the ITuple indexer
                //    var nextPart = part.NextPart;
                //    var name = nextPart.Content;
                //    var index = tupleEltNames.TransformNames.IndexOf(name);
                //    if (index >= 0)
                //    {
                //        nextPart.IsIndexer = true;
                //        nextPart.Content = index.ToString();
                //    }
                //}
#endif
            }
        }
Example #5
0
 public PropertyInfo[] GetAllPublicProperties(TypeInfo typeInfo)
 {
     return typeInfo.GetProperties(BindingFlags.Public | BindingFlags.Instance);
 }