public static T CreateMetadataObject <T>(Type recordType) where T : class { T callbackRecord = default(T); try { MetadataTypeAttribute attr = recordType.GetCustomAttribute <MetadataTypeAttribute>(); if (attr == null) { if (typeof(T).IsAssignableFrom(recordType)) { callbackRecord = ChoActivator.CreateInstance(recordType) as T; } } else { if (attr.MetadataClassType != null && typeof(T).IsAssignableFrom(attr.MetadataClassType)) { callbackRecord = ChoActivator.CreateInstance(attr.MetadataClassType) as T; } } } catch { } return(callbackRecord); }
static ChoMetadataTypesRegister() { foreach (Type type in ChoType.GetTypes(typeof(MetadataTypeAttribute))) { MetadataTypeAttribute attrib = type.GetCustomAttribute <MetadataTypeAttribute>(); if (attrib == null || attrib.MetadataClassType == null) { continue; } TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(type, attrib.MetadataClassType), type); } foreach (Type type in ChoType.GetTypes(typeof(ChoMetadataRefTypeAttribute))) { ChoMetadataRefTypeAttribute attrib = type.GetCustomAttribute <ChoMetadataRefTypeAttribute>(); if (attrib == null || attrib.MetadataRefClassType == null) { continue; } TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(attrib.MetadataRefClassType, type), attrib.MetadataRefClassType); } }
static ChoMetadataTypesRegister() { foreach (Type type in ChoType.GetTypes(typeof(MetadataTypeAttribute))) { MetadataTypeAttribute attrib = ChoType.GetAttribute <MetadataTypeAttribute>(type); TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(type, attrib.MetadataClassType), type); } }
public static Type GetAssociatedMetadataType(Type type) { Type associatedMetadataType = null; if (_metadataTypeCache.TryGetValue(type, out associatedMetadataType)) { return(associatedMetadataType); } // Try association attribute MetadataTypeAttribute attribute = (MetadataTypeAttribute)Attribute.GetCustomAttribute(type, typeof(MetadataTypeAttribute)); if (attribute != null) { associatedMetadataType = attribute.MetadataClassType; } _metadataTypeCache.TryAdd(type, associatedMetadataType); return(associatedMetadataType); }
public object GetMetadataObject(object @this) { if (@this == null) { return(@this); } Type type = @this.GetType(); if (_objectCache.ContainsKey(type)) { return(_objectCache[type] != null ? _objectCache[type] : @this); } MetadataTypeAttribute attr = type.GetCustomAttribute <MetadataTypeAttribute>(); if (attr == null || attr.MetadataClassType == null) { return(@this); } else { lock (_padLock) { if (!_objectCache.ContainsKey(type)) { object obj = null; try { obj = ChoActivator.CreateInstanceAndInit(attr.MetadataClassType); } catch { } _objectCache.Add(type, obj); } return(_objectCache[type] != null ? _objectCache[type] : @this); } } }
public static void Register(Type type) { if (type == null) { return; } MetadataTypeAttribute attrib = type.GetCustomAttribute <MetadataTypeAttribute>(); if (attrib != null && attrib.MetadataClassType != null) { Register(type, attrib.MetadataClassType); } else { ChoMetadataRefTypeAttribute attrib1 = type.GetCustomAttribute <ChoMetadataRefTypeAttribute>(); if (attrib1 != null && attrib1.MetadataRefClassType != null) { Register(attrib1.MetadataRefClassType, type); } } }