public static object CreateMoqInstance(Type objType, ChoIniFile iniFile = null) { //if (typeof(ChoRecord).IsAssignableFrom(objType)) // return CreateDynamicMoqInstance(objType, iniFile); object obj = ChoActivator.CreateInstanceAndInit(objType); foreach (KeyValuePair <MemberInfo, Attribute> kv in ChoUtility.DiscoverMembers(objType, typeof(ChoRandomAttribute))) { if (!(kv.Value is ChoRandomAttribute)) { continue; } ChoType.SetMemberValue(obj, kv.Key, ((ChoRandomAttribute)kv.Value).NextValue()); } return(obj); }
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 object ChangeType <T>(object value, Type conversionType) where T : Attribute { if (value == null) { return(ChoActivator.CreateInstanceAndInit(conversionType)); } if (conversionType.IsAssignableFrom(value.GetType())) { return(value); } object dest = null; if (value is IConvertible) { dest = Convert.ChangeType(value, conversionType); } else { dest = ChoActivator.CreateInstanceAndInit(conversionType); value.CloneTo <T>(dest); } if (dest != null && !dest.GetType().IsSimple()) { ChoObjectValidationMode m = GetValidationMode(value); if (m == ChoObjectValidationMode.MemberLevel) { ChoValidator.Validate(dest); } else if (m == ChoObjectValidationMode.ObjectLevel) { ChoValidator.Validate(dest); } } return(dest); }