public void TheConvertMethod(Type type, string memberName, string expectedValue) { var actualValue = string.Empty; if (string.IsNullOrWhiteSpace(memberName)) { actualValue = ConvertValue(type); Assert.AreEqual(expectedValue, actualValue); return; } var propertyInfo = type.GetPropertyEx(memberName); if (propertyInfo != null) { actualValue = ConvertValue(propertyInfo); Assert.AreEqual(expectedValue, actualValue); return; } var fieldInfo = type.GetFieldEx(memberName); if (fieldInfo != null) { actualValue = ConvertValue(fieldInfo); Assert.AreEqual(expectedValue, actualValue); return; } // This must be an enum var enumValue = Enum.Parse(type, memberName); actualValue = ConvertValue(enumValue); Assert.AreEqual(expectedValue, actualValue); }
protected override IBindingMemberInfo GetExplicitBindingMember(Type sourceType, string path) { if (typeof(DependencyObject).IsAssignableFrom(sourceType)) { var property = GetDependencyProperty(sourceType, path); if (property != null) { IBindingMemberInfo updateEvent = BindingServiceProvider.UpdateEventFinder(sourceType, path); #if WPF return new DependencyPropertyBindingMember(property, path, property.PropertyType, property.ReadOnly, sourceType.GetProperty(path), updateEvent); #else var member = sourceType.GetPropertyEx(path); Type type = typeof(object); bool readOnly = false; if (member != null) { type = member.PropertyType; readOnly = !member.CanWrite; } return new DependencyPropertyBindingMember(property, path, type, readOnly, member, updateEvent); #endif } } return base.GetExplicitBindingMember(sourceType, path); }
/// <summary> /// Initializes a new instance of the <see cref="SerializationModelInfo"/> class. /// </summary> /// <param name="modelType">Type of the model.</param> /// <param name="catelProperties">The catel properties.</param> /// <param name="fields">The fields.</param> /// <param name="regularProperties">The properties.</param> public SerializationModelInfo(Type modelType, Dictionary<string, MemberMetadata> catelProperties, Dictionary<string, MemberMetadata> fields, Dictionary<string, MemberMetadata> regularProperties) { Argument.IsNotNull("modelType", modelType); Argument.IsNotNull("catelProperties", catelProperties); Argument.IsNotNull("fields", fields); Argument.IsNotNull("properties", regularProperties); ModelType = modelType; CatelTypeInfo catelTypeInfo = null; CatelPropertyNames = new HashSet<string>(catelProperties.Keys); CatelProperties = new List<PropertyData>(); CatelPropertiesByName = catelProperties; foreach (var catelProperty in catelProperties) { var propertyData = catelProperty.Value.Tag as PropertyData; if (propertyData == null) { if (catelTypeInfo == null) { catelTypeInfo = PropertyDataManager.Default.GetCatelTypeInfo(modelType); } propertyData = catelTypeInfo.GetPropertyData(catelProperty.Key); } CatelProperties.Add(propertyData); } FieldNames = new HashSet<string>(fields.Keys); Fields = new List<FieldInfo>(); FieldsByName = fields; foreach (var field in fields) { var fieldInfo = field.Value.Tag as FieldInfo; if (fieldInfo == null) { fieldInfo = modelType.GetFieldEx(field.Key); } Fields.Add(fieldInfo); } PropertyNames = new HashSet<string>(regularProperties.Keys); Properties = new List<PropertyInfo>(); PropertiesByName = regularProperties; foreach (var regularProperty in regularProperties) { var propertyInfo = regularProperty.Value.Tag as PropertyInfo; if (propertyInfo == null) { propertyInfo = modelType.GetPropertyEx(regularProperty.Key); } Properties.Add(propertyInfo); } }
public void ReturnsAttributeForMembers(Type type, Type expectedAttributeType, bool isNotNull) { var member = type.GetPropertyEx("Property"); var attribute = member.GetAttribute(expectedAttributeType); if (isNotNull) { Assert.IsNotNull(attribute); } else { Assert.IsNull(attribute); } }
private static Func<string> FindResourceAccessor(string resourceName, Type resourceType) { PropertyInfo propertyInfo = resourceType.GetPropertyEx(resourceName, MemberFlags.Static | MemberFlags.Public | MemberFlags.NonPublic); if (propertyInfo == null) throw ExceptionManager.ResourceNotFound(resourceName, resourceType); if (propertyInfo.PropertyType != typeof(string)) throw ExceptionManager.ResourceNotString(resourceName, resourceType); MethodInfo methodInfo = propertyInfo.GetGetMethod(true); if (methodInfo == null) throw ExceptionManager.ResourceHasNotGetter(resourceName, resourceType); return (Func<string>)ServiceProvider.ReflectionManager.GetMethodDelegate(typeof(Func<string>), methodInfo); }
/// <summary> /// Initializes a new instance of the <see cref="ViewModelPropertyDescriptor" /> class. /// </summary> /// <param name="viewModel">The view model. Can be <c>null</c> for generic property definitions.</param> /// <param name="propertyName">Name of the property.</param> /// <param name="propertyType">Type of the property.</param> /// <param name="attributes">The attributes.</param> /// <exception cref="ArgumentException">The <paramref name="propertyName" /> is <c>null</c> or whitespace.</exception> /// <exception cref="ArgumentNullException">The <paramref name="propertyType" /> is <c>null</c>.</exception> /// <remarks>Must be kept internal because it contains special generic options such as a null view model.</remarks> internal ViewModelPropertyDescriptor(ViewModelBase viewModel, string propertyName, Type propertyType, Attribute[] attributes) : base(propertyName, attributes) { Argument.IsNotNullOrWhitespace("propertyName", propertyName); Argument.IsNotNull("propertyType", propertyType); _viewModel = viewModel; _viewModelType = (viewModel != null) ? viewModel.GetType() : null; _propertyName = propertyName; _propertyType = propertyType; if (_viewModelType != null) { string cacheKey = string.Format("{0}_{1}", _viewModelType.FullName, propertyName); _propertyInfo = _propertyInfoCache.GetFromCacheOrFetch(cacheKey, () => _viewModelType.GetPropertyEx(propertyName)); } }
/// <summary> /// Initializes a new instance of the <see cref="SerializationModelInfo"/> class. /// </summary> /// <param name="modelType">Type of the model.</param> /// <param name="catelPropertyNames">The catel property names.</param> /// <param name="fieldNames">The fields.</param> /// <param name="propertyNames">The properties.</param> public SerializationModelInfo(Type modelType, HashSet<string> catelPropertyNames, HashSet<string> fieldNames, HashSet<string> propertyNames) { Argument.IsNotNull("modelType", modelType); Argument.IsNotNull("catelPropertyNames", catelPropertyNames); Argument.IsNotNull("fields", fieldNames); Argument.IsNotNull("properties", propertyNames); ModelType = modelType; var catelTypeInfo = PropertyDataManager.Default.GetCatelTypeInfo(modelType); CatelPropertyNames = catelPropertyNames; CatelProperties = new List<PropertyData>(); CatelPropertiesByName = new Dictionary<string, PropertyData>(); foreach (var catelPropertyName in catelPropertyNames) { var propertyData = catelTypeInfo.GetPropertyData(catelPropertyName); CatelProperties.Add(propertyData); CatelPropertiesByName[propertyData.Name] = propertyData; } FieldNames = fieldNames; Fields = new List<FieldInfo>(); FieldsByName = new Dictionary<string, FieldInfo>(); foreach (var fieldName in fieldNames) { var fieldInfo = modelType.GetFieldEx(fieldName); Fields.Add(fieldInfo); FieldsByName[fieldName] = fieldInfo; } PropertyNames = propertyNames; Properties = new List<PropertyInfo>(); PropertiesByName = new Dictionary<string, PropertyInfo>(); foreach (var propertyName in propertyNames) { var propertyInfo = modelType.GetPropertyEx(propertyName); Properties.Add(propertyInfo); PropertiesByName[propertyName] = propertyInfo; } }
/// <summary> /// Initializes a new instance of the <see cref="ViewModelPropertyDescriptor"/> class. /// </summary> /// <param name="viewModel">The view model. Can be <c>null</c> for generic property definitions.</param> /// <param name="propertyName">Name of the property.</param> /// <param name="propertyType">Type of the property.</param> /// <remarks> /// Must be kept internal because it contains special generic options such as a null view model. /// </remarks> /// <exception cref="ArgumentException">The <paramref name="propertyName"/> is <c>null</c> or whitespace.</exception> /// <exception cref="ArgumentNullException">The <paramref name="propertyType"/> is <c>null</c>.</exception> internal ViewModelPropertyDescriptor(ViewModelBase viewModel, string propertyName, Type propertyType) : base(propertyName, null) { Argument.IsNotNullOrWhitespace("propertyName", propertyName); Argument.IsNotNull("propertyType", propertyType); //Log.Debug("Created property descriptor for '{0}' as type '{1}'", propertyName, propertyType.Name); _viewModel = viewModel; _viewModelType = (viewModel != null) ? viewModel.GetType() : null; _propertyName = propertyName; _propertyType = propertyType; if (_viewModelType != null) { string cacheKey = string.Format("{0}_{1}", _viewModelType.FullName, propertyName); _propertyInfo = _propertyInfoCache.GetFromCacheOrFetch(cacheKey, () => _viewModelType.GetPropertyEx(propertyName)); } }
private static DependencyProperty GetDependencyProperty(Type type, string name) { #if WPF DependencyPropertyDescriptor descriptor = DependencyPropertyDescriptor.FromName(name, type, type); if (descriptor == null) return null; return descriptor.DependencyProperty; #else FieldInfo fieldInfo = type.GetFieldEx(name + "Property", MemberFlags.Public | MemberFlags.Static) ?? type.GetFieldEx(name, MemberFlags.Public | MemberFlags.Static); var property = fieldInfo == null ? null : fieldInfo.GetValue(null) as DependencyProperty; if (property == null) { var prop = type.GetPropertyEx(name + "Property", MemberFlags.Public | MemberFlags.Static) ?? type.GetPropertyEx(name, MemberFlags.Public | MemberFlags.Static); property = (prop == null || !prop.CanRead) ? null : prop.GetValue(null, null) as DependencyProperty; } return property; #endif }
/// <summary> /// Gets the regular properties. /// </summary> /// <param name="type">Type of the model.</param> /// <returns>A hash set containing the regular properties.</returns> /// <exception cref="ArgumentNullException">The <paramref name="type"/> is <c>null</c>.</exception> public Dictionary<string, MemberMetadata> GetRegularProperties(Type type) { Argument.IsNotNull("type", type); return _regularPropertiesCache.GetFromCacheOrFetch(type, () => { var dictionary = new Dictionary<string, MemberMetadata>(); var regularPropertyNames = GetRegularPropertyNames(type); foreach (var property in regularPropertyNames) { var propertyInfo = type.GetPropertyEx(property); if (propertyInfo != null) { dictionary[property] = new MemberMetadata(type, propertyInfo.PropertyType, SerializationMemberGroup.RegularProperty, property); } } return dictionary; }); }
/// <summary> /// Gets the properties to serialize for the specified object. /// </summary> /// <param name="type">The type.</param> /// <returns>The list of properties to serialize.</returns> /// <exception cref="ArgumentNullException">The <paramref name="type"/> is <c>null</c>.</exception> public virtual HashSet<string> GetPropertiesToSerialize(Type type) { Argument.IsNotNull("type", type); return _propertiesToSerializeCache.GetFromCacheOrFetch(type, () => { var properties = new List<string>(); var propertyDataManager = PropertyDataManager.Default; var catelTypeInfo = propertyDataManager.GetCatelTypeInfo(type); var catelProperties = catelTypeInfo.GetCatelProperties(); var catelPropertyNames = catelProperties.Keys.ToList(); foreach (var modelProperty in catelProperties) { var propertyData = modelProperty.Value; if (!propertyData.IsSerializable) { Log.Warning("Property '{0}' is not serializable, so will be excluded from the serialization", propertyData.Name); continue; } if (!propertyData.IncludeInSerialization) { Log.Debug("Property '{0}' is flagged to be excluded from serialization", propertyData.Name); continue; } var propertyType = type.GetPropertyEx(modelProperty.Value.Name); if (propertyType != null) { if (!AttributeHelper.IsDecoratedWithAttribute<ExcludeFromSerializationAttribute>(propertyType)) { properties.Add(modelProperty.Key); } } else { // Dynamic property, always include properties.Add(modelProperty.Key); } } var typeProperties = type.GetPropertiesEx(); foreach (var typeProperty in typeProperties) { if (!catelPropertyNames.Contains(typeProperty.Name)) { if (AttributeHelper.IsDecoratedWithAttribute<IncludeInSerializationAttribute>(typeProperty)) { properties.Add(typeProperty.Name); } } } return new HashSet<string>(properties); }); }
/// <summary> /// Returns the sequence of properties of the specified type. /// </summary> /// <param name="type">The type.</param> /// <param name="schema">The schema.</param> /// <param name="schemaSet">The schema set.</param> /// <param name="serializationManager">The serialization manager.</param> /// <returns>Sequence containing all properties.</returns> private static XmlSchemaSequence GetPropertiesSequence(Type type, XmlSchema schema, XmlSchemaSet schemaSet, ISerializationManager serializationManager) { Argument.IsNotNull("type", type); Argument.IsNotNull("schema", schema); Argument.IsNotNull("schemaSet", schemaSet); var propertiesSequence = new XmlSchemaSequence(); if (typeof(ModelBase).IsAssignableFromEx(type)) { var typeNs = GetTypeNamespaceForSchema(type); var members = new List<MemberInfo>(); members.AddRange(from field in serializationManager.GetFieldsToSerialize(type) select type.GetFieldEx(field)); members.AddRange(from property in serializationManager.GetPropertiesToSerialize(type) select type.GetPropertyEx(property)); foreach (var member in members) { var propertySchemaElement = new XmlSchemaElement(); propertySchemaElement.Name = member.Name; var memberType = typeof(object); var fieldInfo = member as FieldInfo; if (fieldInfo != null) { memberType = fieldInfo.FieldType; } var propertyInfo = member as PropertyInfo; if (propertyInfo != null) { memberType = propertyInfo.PropertyType; } if (memberType.ImplementsInterfaceEx(typeof(IEnumerable)) && memberType != typeof(string)) { propertySchemaElement.SchemaTypeName = new XmlQualifiedName(string.Format("{0}", member.Name), typeNs); var collectionPropertyType = new XmlSchemaComplexType(); collectionPropertyType.Name = string.Format("{0}", member.Name); schema.Items.Add(collectionPropertyType); foreach (var genericArgument in memberType.GetGenericArguments()) { AddTypeToSchemaSet(genericArgument, schemaSet, serializationManager); } } else { propertySchemaElement.SchemaTypeName = AddTypeToSchemaSet(memberType, schemaSet, serializationManager); propertySchemaElement.IsNillable = TypeHelper.IsTypeNullable(memberType); propertySchemaElement.MinOccurs = 0; } propertiesSequence.Items.Add(propertySchemaElement); } } return propertiesSequence; }
/// <summary> /// Returns the sequence of properties of the specified type. /// </summary> /// <param name="type">The type.</param> /// <param name="schema">The schema.</param> /// <param name="schemaSet">The schema set.</param> /// <param name="serializationManager">The serialization manager.</param> /// <returns>Sequence containing all properties.</returns> private static XmlSchemaSequence GetPropertiesSequence(Type type, XmlSchema schema, XmlSchemaSet schemaSet, ISerializationManager serializationManager) { Argument.IsNotNull("type", type); Argument.IsNotNull("schema", schema); Argument.IsNotNull("schemaSet", schemaSet); var propertiesSequence = new XmlSchemaSequence(); if (typeof(ModelBase).IsAssignableFromEx(type)) { var members = new List<MemberInfo>(); members.AddRange(from field in serializationManager.GetFieldsToSerialize(type) select type.GetFieldEx(field)); members.AddRange(from property in serializationManager.GetPropertiesToSerialize(type) select type.GetPropertyEx(property)); foreach (var member in members) { var propertySchemaElement = new XmlSchemaElement(); propertySchemaElement.Name = member.Name; var memberType = typeof(object); var fieldInfo = member as FieldInfo; if (fieldInfo != null) { memberType = fieldInfo.FieldType; } var propertyInfo = member as PropertyInfo; if (propertyInfo != null) { memberType = propertyInfo.PropertyType; } propertySchemaElement.IsNillable = memberType.IsNullableType(); propertySchemaElement.MinOccurs = 0; var exporter = new XsdDataContractExporter(schemaSet); exporter.Export(memberType); propertySchemaElement.SchemaType = exporter.GetSchemaType(memberType); propertySchemaElement.SchemaTypeName = exporter.GetSchemaTypeName(memberType); propertiesSequence.Items.Add(propertySchemaElement); } } return propertiesSequence; }