/// <summary> /// 这里有个问题是:如果不修改类的名字,则重新创建时会出现同名程序集则不能创建,所以有任何更新都必须更新类名。但是更新了类名就无法做到数据迁移 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static bool CompareRuntimeMeta(RuntimeModelMeta a, RuntimeModelMeta b) { if (a.ModelId != b.ModelId) { return(false); } if (a.ModelName != b.ModelName) { return(false); } if (a.ClassName != b.ClassName) { return(false); } if (a.ModelProperties.Length != b.ModelProperties.Length) { return(false); } for (int Count = 0; Count < a.ModelProperties.Length; Count++) { bool Isthere = false; for (int num = 0; num < b.ModelProperties.Length; num++) { if (a.ModelProperties[Count].Name != b.ModelProperties[Count].Name) { break; } if (a.ModelProperties[Count].PropertyName != b.ModelProperties[Count].PropertyName) { break; } if (a.ModelProperties[Count].Length != b.ModelProperties[Count].Length) { break; } if (a.ModelProperties[Count].IsRequired != b.ModelProperties[Count].IsRequired) { break; } if (a.ModelProperties[Count].ValueType != b.ModelProperties[Count].ValueType) { break; } Isthere = true; } if (Isthere == false) { return(false); } } return(true); }
public static IRuntimeModelProvider CreateInstance(string strjson) { RuntimeModelMeta Temp = Newtonsoft.Json.JsonConvert.DeserializeObject <RuntimeModelMeta>(strjson); if (_instance == null || !CompareRuntimeMeta(Temp, TempCache)) { TempCache = Temp; lock (lockHelper) { _instance = new DefaultRuntimeModelProvider(new RuntimeModelMetaConfig() { Metas = new RuntimeModelMeta[] { TempCache } }); } } return(_instance); }
//这个方法就是把一个RuntimeModelMeta转换成更接近类结构的TypeMeta对象 private TypeMeta GetTypeMetaFromModelMeta(RuntimeModelMeta meta) { TypeMeta typeMeta = new TypeMeta(); //我们让所有的动态类型都继承自DynamicEntity类,这个类主要是为了方便属性数据的读取,具体代码看后面 typeMeta.BaseType = typeof(DynamicEntity); typeMeta.TypeName = meta.ClassName; foreach (var item in meta.ModelProperties) { TypeMeta.TypePropertyMeta pmeta = new TypeMeta.TypePropertyMeta(); pmeta.PropertyName = item.PropertyName; //如果必须输入数据,我们在属性上增加RequireAttribute特性,这样方便我们进行数据验证 if (item.IsRequired) { TypeMeta.AttributeMeta am = new TypeMeta.AttributeMeta(); am.AttributeType = typeof(RequiredAttribute); am.Properties = new string[] { "ErrorMessage" }; am.PropertyValues = new object[] { "请输入" + item.Name }; pmeta.AttributeMetas.Add(am); } if (item.ValueType == "string") { pmeta.PropertyType = typeof(string); TypeMeta.AttributeMeta am = new TypeMeta.AttributeMeta(); //增加长度验证特性 am.AttributeType = typeof(StringLengthAttribute); am.ConstructorArgTypes = new Type[] { typeof(int) }; am.ConstructorArgValues = new object[] { item.Length }; am.Properties = new string[] { "ErrorMessage" }; am.PropertyValues = new object[] { item.Name + "长度不能超过" + item.Length.ToString() + "个字符" }; pmeta.AttributeMetas.Add(am); } else if (item.ValueType == "int") { if (!item.IsRequired) { pmeta.PropertyType = typeof(int?); } else { pmeta.PropertyType = typeof(int); } } else if (item.ValueType == "datetime") { if (!item.IsRequired) { pmeta.PropertyType = typeof(DateTime?); } else { pmeta.PropertyType = typeof(DateTime); } } else if (item.ValueType == "bool") { if (!item.IsRequired) { pmeta.PropertyType = typeof(bool?); } else { pmeta.PropertyType = typeof(bool); } } typeMeta.PropertyMetas.Add(pmeta); } return(typeMeta); }