GetDeclaredMethod() public method

public GetDeclaredMethod ( string name ) : MethodInfo
name string
return MethodInfo
            public ReflectionNullabilityResolver()
            {
                nullableAnnotation = typeSymbolInfo.GetDeclaredProperty("NullableAnnotation") !.GetMethod;
                withoutNullable    = typeSymbolInfo.GetDeclaredMethod("WithNullableAnnotation");
                var nullableAnnotations = Enum.GetValues(withoutNullable.GetParameters()[0].ParameterType);

                annotatedValue      = nullableAnnotations.GetValue(2);
                nonValue            = nullableAnnotations.GetValue(0);
                stripValueParameter = new[] { nonValue };
            }
Esempio n. 2
0
        public void ResolveMethod_should_return_original_method_info()
        {
            var typeResolver = new TypeResolver();

            System.Reflection.TypeInfo   type           = typeof(TypeHiding).GetTypeInfo();
            System.Reflection.MethodInfo methodInfo     = type.GetDeclaredMethod(nameof(TypeHiding.Method));
            Aqua.TypeSystem.MethodInfo   mappedMethod   = new Aqua.TypeSystem.MethodInfo(methodInfo);
            System.Reflection.MethodInfo resolvedMethod = mappedMethod.ResolveMethod(typeResolver);

            resolvedMethod.ShouldBe(methodInfo);
        }
Esempio n. 3
0
 internal static bool HasOverriddenToString(System.Reflection.TypeInfo type)
 {
     if (type.IsInterface)
     {
         return(false);
     }
     for (; type.AsType() != (object)typeof(object); type = IntrospectionExtensions.GetTypeInfo(type.BaseType))
     {
         if ((object)type.GetDeclaredMethod("ToString") != null)
         {
             return(true);
         }
     }
     return(false);
 }
Esempio n. 4
0
        private static bool HasOverriddenToString(TypeInfo type)
        {
            if (type.IsInterface)
            {
                return false;
            }

            while (type.AsType() != typeof(object))
            {
                if (type.GetDeclaredMethod("ToString", Type.EmptyTypes) != null)
                {
                    return true;
                }

                type = type.BaseType.GetTypeInfo();
            }

            return false;
        }
Esempio n. 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
            }
        }
Esempio n. 6
0
 public static MethodInfo TryGetMethodInHierarchy(TypeInfo ti, string name)
 {
     while (true)
     {
         var mi = ti.GetDeclaredMethod(name);
         if (mi != null)
         {
             return mi;
         }
         var baseType = ti.BaseType;
         if (baseType == null)
         {
             return null;
         }
         ti = baseType.GetTypeInfo();
     }
 }
Esempio n. 7
0
 public static MethodInfo GetMethod(TypeInfo ti, string name)
 {
     var r = ti.GetDeclaredMethod(name);
     if (r != null) return r;
     throw new ArgumentException("Cannot get method: " + name);
 }
Esempio n. 8
0
 // an (awful) test for a null read-only-array.  Is there no better way to do this?
 private static bool IsDefaultImmutableArray(Object o)
 {
     System.Reflection.TypeInfo ti = o.GetType().GetTypeInfo();
     return(ti.IsGenericType && ti.GetGenericTypeDefinition() == typeof(ImmutableArray <>) &&
            (bool)ti.GetDeclaredMethod("get_IsDefault").Invoke(o, Array.Empty <object>()));
 }
Esempio n. 9
0
 public PagingParser()
 {
     pagingExtensionsType = typeof(PagingExtensions).GetTypeInfo();
     skipMethod = pagingExtensionsType.GetDeclaredMethod("AddSkip");
     takeMethod = pagingExtensionsType.GetDeclaredMethod("AddTake");
 }
Esempio n. 10
0
 public SelectParser()
 {
     projectionType = typeof(SelectParser).GetTypeInfo();
     selectMethod = projectionType.GetDeclaredMethod("AddSelect");
 }
Esempio n. 11
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);
							}
						}
					}
				}
			}
		}
        /// <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);
            }
        }