/// <summary> /// Tries to convert the specified value to the specified value. /// </summary> /// <param name="rawValue">Value to convert.</param> /// <param name="type">Type to convert to.</param> /// <param name="convertedValue">Converted value.</param> /// <returns>True if value could be converted; otherwise, false.</returns> public static bool TryConvertValue(object rawValue, Type type, out object convertedValue) { try { // Try convert enum. if (TypeInfoUtils.IsEnum(type)) { if (!Enum.IsDefined(type, rawValue)) { convertedValue = null; return(false); } var stringValue = rawValue as string; convertedValue = stringValue != null?Enum.Parse(type, stringValue) : Enum.ToObject(type, rawValue); return(true); } convertedValue = Convert.ChangeType(rawValue, type); return(true); } catch (Exception) { convertedValue = null; return(false); } }
/// <summary> /// Returns the data provider of the specified object and type with the specified name. /// </summary> /// <param name="type">Type to search.</param> /// <param name="obj">Concrete object to get data provider for.</param> /// <param name="name">Name of data to get data provider for.</param> /// <returns>Data provider for the specified object and name, using the specified type for reflection.</returns> private static IDataProvider GetDataProvider(Type type, object obj, string name) { var settings = Settings; var dataProviderName = settings.GetDataProviderName(name); var additionalFlags = BindingFlags.Default; if (settings.DataProviderIsCaseInsensitive) { additionalFlags |= BindingFlags.IgnoreCase; } // Check for field. var dataProviderField = TypeInfoUtils.GetPrivateField(type, dataProviderName, additionalFlags); if (dataProviderField != null) { return(dataProviderField.GetValue(obj) as IDataProvider); } // Check for public property. var dataProviderProperty = TypeInfoUtils.GetPublicProperty(type, dataProviderName, additionalFlags); if (dataProviderProperty != null) { return(dataProviderProperty.GetValue(obj, null) as IDataProvider); } return(null); }
/// <inheritdoc /> public override object GetValue(object parentObject) { if (parentObject == null) { return(null); } // Get delegate. if (this.Method != null) { var args = new List <Type>(this.Method.GetParameters().Select(p => p.ParameterType)); var delegateType = Expression.GetActionType(args.ToArray()); return(TypeInfoUtils.CreateDelegate(delegateType, parentObject, this.Method)); } return(null); }
/// <summary> /// Returns the data provider of the specified object and type with the specified name. /// </summary> /// <param name="obj">Concrete object to get data provider for.</param> /// <param name="name">Name of data provider to get.</param> /// <returns>Data provider for the specified object and name.</returns> public static IDataProvider GetDataProvider(object obj, string name) { if (obj == null || string.IsNullOrEmpty(name)) { return(null); } // Go up the type hierarchy of the object. // NOTE(co): Private fields of base classes are not reflected for derived types. var type = obj.GetType(); while (type != null) { var property = GetDataProvider(type, obj, name); if (property != null) { return(property); } type = TypeInfoUtils.GetBaseType(type); } return(null); }
/// <summary> /// Searches all loaded assemblies and returns the types which have the specified attribute. /// </summary> /// <returns>List of found types.</returns> /// <typeparam name="T">Type of the attribute to get the types of.</typeparam> public static IEnumerable <Type> FindTypesWithBase <T>() where T : class { return(TypeInfoUtils.FindTypesWithBase(typeof(T))); }
/// <summary> /// Returns the type info for the member with the specified name of the specified type. /// </summary> /// <param name="type">Type to get member info from.</param> /// <param name="name">Name of member to get info for.</param> /// <returns>Type info for the member with the specified name of the specified type.</returns> public static NodeTypeInfo GetNodeTypeInfo(Type type, string name) { // Get item if collection. var typeInterfaces = TypeInfoUtils.GetInterfaces(type); if (typeInterfaces.Contains(typeof(IEnumerable))) { // Check if index provided. int itemIndex; if (int.TryParse(name, out itemIndex)) { // Get item type. var itemType = type.GetElementType(); if (itemType == null) { if (TypeInfoUtils.IsGenericType(type)) { var genericArguments = TypeInfoUtils.GetGenericArguments(type); itemType = genericArguments.Length > 0 ? genericArguments[0] : typeof(object); } else { itemType = typeof(object); } } // Return item. return(new EnumerableNode { Type = itemType, Index = itemIndex }); } } // Get property. var reflectionProperty = TypeInfoUtils.GetPublicProperty(type, name); if (reflectionProperty != null) { return(new PropertyNode { Type = reflectionProperty.PropertyType, Property = reflectionProperty }); } // Get field. var reflectionField = TypeInfoUtils.GetPublicField(type, name); if (reflectionField != null) { return(new FieldNode { Type = reflectionField.FieldType, Field = reflectionField }); } // Get method. var reflectionMethod = TypeInfoUtils.GetPublicMethod(type, name); if (reflectionMethod != null) { return(new MethodNode { Type = reflectionMethod.ReturnType, Method = reflectionMethod }); } return(null); }
/// <summary> /// Returns the full name of the specified type without any assembly version info. /// The reason why this method is needed is that a generic type's FullName contains /// the full AssemblyQualifiedName of its item type. /// </summary> /// <param name="type">Type to get full name for.</param> /// <returns>Full name of specified type without additional info of the assembly.</returns> public static string FullNameWithoutAssemblyInfo(this Type type) { return(!TypeInfoUtils.IsGenericType(type) ? type.FullName : RemoveAssemblyInfo(type.FullName)); }