/// <summary> /// Creates the context. /// </summary> /// <returns></returns> public static TypeMetaData GetFor(Type wrapperObjectType, Type wrappedObjectType) { lock (_contextsLock) { if (!_contexts.ContainsKey(wrapperObjectType)) { TypeMetaData result = new TypeMetaData(); List<string> propertyNames = new List<string>(); foreach (PropertyInfo property in wrapperObjectType.GetProperties()) { if (property.GetCustomAttributes(true).Where(a => a is BrowsableAttribute && ((BrowsableAttribute)a).Browsable == false).Count() == 0) { result.PropertyDescriptors.Add(CreateWrapperObjectPropertyWrapper(result, property)); result.AllKnownProperties.Add(property); propertyNames.Add(property.Name); } } foreach (PropertyInfo property in wrappedObjectType.GetProperties()) { if (!propertyNames.Contains(property.Name)) { result.PropertyDescriptors.Add(CreateWrappedObjectPropertyWrapper(result, property)); result.AllKnownProperties.Add(property); } } _contexts.Add(wrapperObjectType, result); } return _contexts[wrapperObjectType]; } }
/// <summary> /// Creates the wrapper object property wrapper. /// </summary> private static PropertyDescriptor CreateWrapperObjectPropertyWrapper(TypeMetaData context, PropertyInfo property) { IDelegatePropertyReader actualReader = GetPropertyReader(property, context); IDelegatePropertyWriter actualWriter = GetPropertyWriter(property, context); Func<object, object> reader = property.CanRead ? new Func<object, object>(o => actualReader.GetValue(o)) : null; Action<object, object> writer = property.CanWrite ? new Action<object, object>((o, v) => actualWriter.SetValue(o, v)) : null; DelegatePropertyDescriptor descriptor = new DelegatePropertyDescriptor( property.Name, property.DeclaringType, property.PropertyType, reader, writer ); return descriptor; }
/// <summary> /// Initializes a new instance of the <see cref="DesignTimeTypeDescriptor"/> class. /// </summary> /// <param name="objectType">Type of the object.</param> /// <param name="instance">The instance.</param> public EditableTypeDescriptor(Type objectType) { Type wrapperObjectType = objectType; Type wrappedObjectType = objectType; Type baseType = wrapperObjectType.BaseType; while (baseType != null && baseType != typeof(object)) { if (baseType.Name == typeof(EditableAdapter<>).Name && baseType.GetGenericArguments().Length > 0) { wrappedObjectType = baseType.GetGenericArguments()[0]; break; } baseType = baseType.BaseType; } _context = TypeMetaDataRepository.GetFor(wrapperObjectType, wrappedObjectType); }
/// <summary> /// Gets the property writer. /// </summary> private static IDelegatePropertyWriter GetPropertyWriter(PropertyInfo property, TypeMetaData context) { IDelegatePropertyWriter actualWriter = DelegatePropertyFactory.CreatePropertyWriter(property); if (actualWriter != null) { context.PropertyWriters.Add(property, actualWriter); } return(actualWriter); }
/// <summary> /// Gets the property writer. /// </summary> private static IDelegatePropertyWriter GetPropertyWriter(PropertyInfo property, TypeMetaData context) { IDelegatePropertyWriter actualWriter = DelegatePropertyFactory.CreatePropertyWriter(property); if (actualWriter != null) { context.PropertyWriters.Add(property, actualWriter); } return actualWriter; }