GetDeclaredField() public method

public GetDeclaredField ( string name ) : FieldInfo
name string
return FieldInfo
Esempio n. 1
0
        public void ResolveMemberInfo_should_return_original_field_info()
        {
            var typeResolver = new TypeResolver();

            System.Reflection.TypeInfo   type           = typeof(TypeHiding).GetTypeInfo();
            System.Reflection.FieldInfo  fieldInfo      = type.GetDeclaredField(nameof(TypeHiding.Field));
            Aqua.TypeSystem.FieldInfo    mappedField    = new Aqua.TypeSystem.FieldInfo(fieldInfo);
            System.Reflection.MemberInfo resolvedMember = mappedField.ResolveMemberInfo(typeResolver);

            resolvedMember.ShouldBe(fieldInfo);
        }
Esempio n. 2
0
        private static Object CanonicalizeValue(Object value)
        {
            Contract.Assert(value != null);

            if (value is Enum)
            {
                // Blech.
                TypeInfo  enumTypeInfo = value.GetType().GetTypeInfo();
                FieldInfo valueField   = enumTypeInfo.GetDeclaredField("value__");
                value = valueField.GetValue(value);
            }
            return(value);
        }
Esempio n. 3
0
 public static FieldInfo TryGetFieldInHierarchy(TypeInfo ti, string name)
 {
     while (true)
     {
         var fi = ti.GetDeclaredField(name);
         if (fi != null)
         {
             return fi;
         }
         var baseType = ti.BaseType;
         if (baseType == null)
         {
             return null;
         }
         ti = baseType.GetTypeInfo();
     }
 }
Esempio n. 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
            }
        }
Esempio n. 5
0
 public static FieldInfo GetField(TypeInfo ti, string name)
 {
     var r = ti.GetDeclaredField(name);
     if (r != null) return r;
     throw new ArgumentException("Cannot get field: " + name);
 }
        FieldInfo actuallyGetField(TypeInfo typeInfo, string propertyName)
        {
            var current = typeInfo;
            while (current != null) {
                var ret = typeInfo.GetDeclaredField(propertyName);
                if (ret != null && ret.IsStatic) return ret;

                current = current.BaseType.GetTypeInfo();
            }

            return null;
        }
 private static IAuthorize GetAuthorization(TypeInfo typeInfo)
 {
     return (IAuthorize)typeInfo.GetDeclaredField("Instance").GetValue(null);
 }
Esempio n. 8
0
		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);
							}
						}
					}
				}
			}
		}