GetDeclaredProperty() public méthode

public GetDeclaredProperty ( string name ) : PropertyInfo
name string
Résultat PropertyInfo
            private void FormatKeyValuePair(CommonObjectFormatter.Builder result, object obj)
            {
                System.Reflection.TypeInfo typeInfo = IntrospectionExtensions.GetTypeInfo(obj.GetType());
                object obj1 = typeInfo.GetDeclaredProperty("Key").GetValue(obj, Array.Empty <object>());
                object obj2 = typeInfo.GetDeclaredProperty("Value").GetValue(obj, Array.Empty <object>());

                result.AppendGroupOpening();
                result.AppendCollectionItemSeparator(true);
                this.FormatObjectRecursive(result, obj1, false);
                result.AppendCollectionItemSeparator(false);
                this.FormatObjectRecursive(result, obj2, false);
                result.AppendGroupClosing();
            }
        public void AllPublicConcreteTypesShouldBeAnnotatedWithDebuggerDisplay(TypeInfo type)
        {
            var attribute = type.GetCustomAttribute<DebuggerDisplayAttribute>(false);

            Assert.Equal("{DebuggerDisplay, nq}", attribute.Value);

            var debuggerDisplay = type.GetDeclaredProperty("DebuggerDisplay");

            Assert.False(debuggerDisplay.GetAccessors().Any(x => x.IsPublic));
        }
Exemple #3
0
        public void ResolveMemberInfo_should_return_original_property_info()
        {
            var typeResolver = new TypeResolver();

            System.Reflection.TypeInfo     type           = typeof(TypeHiding).GetTypeInfo();
            System.Reflection.PropertyInfo propertyInfo   = type.GetDeclaredProperty(nameof(TypeHiding.Property));
            Aqua.TypeSystem.PropertyInfo   mappedProperty = new Aqua.TypeSystem.PropertyInfo(propertyInfo);
            System.Reflection.MemberInfo   resolvedMember = mappedProperty.ResolveMemberInfo(typeResolver);

            resolvedMember.ShouldBe(propertyInfo);
        }
Exemple #4
0
        public static PropertyInfo GetProperty(this Type type, string name)
        {
            Type t = type;

            while (t != null)
            {
                System.Reflection.TypeInfo ti = t.GetTypeInfo();
                PropertyInfo property         = ti.GetDeclaredProperty(name);
                if (property != null)
                {
                    return(property);
                }

                t = ti.BaseType;
            }

            return(null);
        }
Exemple #5
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
            }
        }
Exemple #6
0
 public static PropertyInfo GetProperty(TypeInfo ti, string name)
 {
     var r = ti.GetDeclaredProperty(name);
     if (r != null) return r;
     throw new ArgumentException("Cannot get property: " + name);
 }
Exemple #7
0
 public static PropertyInfo TryGetPropertyInHierarchy(TypeInfo ti, string name)
 {
     while (true)
     {
         var pi = ti.GetDeclaredProperty(name);
         if (pi != null)
         {
             return pi;
         }
         var baseType = ti.BaseType;
         if (baseType == null)
         {
             return null;
         }
         ti = baseType.GetTypeInfo();
     }
 }
Exemple #8
0
        private static PropertyInfo GetIndexer(TypeInfo typeInfo)
        {
            PropertyInfo indexer;

            for (; typeInfo != null; typeInfo = typeInfo.BaseType?.GetTypeInfo())
            {
                // Check for the default indexer name first to make this faster.
                // This will only be false when a class in VB has a custom indexer name.
                if ((indexer = typeInfo.GetDeclaredProperty(CommonPropertyNames.IndexerName)) != null)
                {
                    return indexer;
                }

                foreach (var property in typeInfo.DeclaredProperties)
                {
                    if (property.GetIndexParameters().Any())
                    {
                        return property;
                    }
                }
            }

            return null;
        }
 public static bool IsAvailable() => typeSymbolInfo.GetDeclaredProperty("NullableAnnotation") != null;
        PropertyInfo actuallyGetProperty(TypeInfo typeInfo, string propertyName)
        {
            var current = typeInfo;
            while (current != null) {
                var ret = typeInfo.GetDeclaredProperty(propertyName);
                if (ret != null && ret.IsStatic()) return ret;

                current = current.BaseType.GetTypeInfo();
            }

            return null;
        }
		void SetupPart(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, out index))
						Log.Warning("Binding", "{0} could not be parsed as an index for a {1}", part.Content, 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;

				property = sourceType.GetDeclaredProperty(indexerName);
				if (property == null)
					property = sourceType.BaseType.GetProperty(indexerName);

				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);
				if (property == null)
					property = sourceType.BaseType.GetProperty(part.Content);
			}

			if (property != null)
			{
				if (property.CanRead && property.GetMethod.IsPublic && !property.GetMethod.IsStatic)
					part.LastGetter = property.GetMethod;
				if (property.CanWrite && 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;
							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;
									}
								}
							}
							if (setValueMethod != null)
							{
								part.LastSetter = setValueMethod;
								part.IsBindablePropertySetter = true;
								part.BindablePropertyField = bindablePropertyField.GetValue(null);
							}
						}
					}
				}
			}
		}
        /// <summary>
        /// Adds the DependencyPropertyInfo object for the given.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="typeInfo">The type info.</param>
        /// <param name="dependencyProperty">The dependency property.</param>
        /// <param name="propertyName">Name of the dependency property.</param>
        /// <param name="propertyList">The non-attached dependency property list for the given type.</param>
        private static void AddDependencyPropertyInfo(
            Type type,
            TypeInfo typeInfo,
            DependencyProperty dependencyProperty,
            string propertyName,
            ref List<DependencyPropertyInfo> propertyList)
        {
            try
            {
                bool? isAttached = null;

                // Check for plain property matching the dependency property.
                if (typeInfo.GetDeclaredProperty(propertyName) == null)
                {
                    isAttached = true;
                }
                else
                {
                    // Check for the Get method typically only specified for attached properties
                    var getMethodName = string.Format("Get{0}", propertyName);
                    var getMethod = typeInfo.GetDeclaredMethod(getMethodName);

                    if (getMethod != null)
                    {
                        isAttached = true;
                    }
                    else
                    {
                        isAttached = false;
                    }
                }

                if (isAttached == true)
                {
                    // Attached property
                    var displayName = string.Format("{0}.{1}", type.Name, propertyName);

                    AttachedProperties.Add(
                        new DependencyPropertyInfo(
                            dependencyProperty,
                            propertyName,
                            type,
                            displayName,
                            true));
                }
                else
                {
                    // non-attached property
                    if (propertyList == null)
                    {
                        propertyList = new List<DependencyPropertyInfo>();
                        DependencyProperties.Add(type, propertyList);
                    }

                    propertyList.Add(
                        new DependencyPropertyInfo(
                            dependencyProperty,
                            propertyName,
                            type,
                            propertyName,
                            false));
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }