public Delegate TypeRouter(NBuildInfo info) { string methodBody = null; // 简单类型 // SimpleType if (info.ArrayBaseType.IsSimpleType()) { methodBody = GenerateSimpleTypeClone(info); } // 多维+复杂 // MultiD+Complex else if (info.ArrayDimensions > 1) { methodBody = GenerateMultiDWithComplexClone(info); } // 1维+复杂 及 锯齿+复杂 // 1D+Complex and Jagged+Complex else { methodBody = GenerateJaggedWithComplexClone2(info); } var action = FastMethodOperator.New .Using("DeepClone") .Using(typeof(Array)) .Param(info.DeclaringType, "oldIns") .MethodBody(methodBody) .Return(info.DeclaringType) .Complie(); return(action); }
public override bool Condition(NBuildInfo srcMember, NBuildInfo destMember) { return((!srcMember.MemberType.IsImplementFrom(destMember.MemberType)) && destMember.MemberType != srcMember.MemberType && srcMember.MemberType.IsImplementFrom(typeof(IDictionary <,>)) && destMember.MemberType.IsImplementFrom <IEnumerable>()); }
public Delegate Create <T>(Type type) { NBuildInfo info = type; info.FatherType = typeof(T); return(TypeRouter(info)); }
/// <summary> /// 多维+复杂 /// MultiD+Complex /// </summary> /// <param name="info"></param> /// <returns></returns> private string GenerateMultiDWithComplexClone(NBuildInfo info) { var sb = new StringBuilder(); var varNameArr = new string[info.ArrayDimensions]; var multiArrLenArr = new string[info.ArrayDimensions]; for (int i = 0; i < info.ArrayDimensions; i++) { var varName = $"_{i}"; varNameArr[i] = varName; multiArrLenArr[i] = $"oldIns.GetLength({i})"; sb.AppendLine($"for (int {varName} = 0; {varName} < oldIns.GetLength({i}); {varName}++)"); } var varNameStr = string.Join(",", varNameArr); var multiArrTypeStr = string.Join(",", multiArrLenArr); var methodBody = $@" if(oldIns==default) return default; {info.DeclaringTypeName} newIns = new {info.ElementTypeName}[{multiArrTypeStr}]; {sb.ToString()} newIns[{varNameStr}] = CloneOperator.Clone(oldIns[{varNameStr}]); return newIns; "; return(methodBody); }
public Delegate TypeRouter(NBuildInfo info) { StringBuilder scriptBuilder = new StringBuilder(); scriptBuilder.AppendLine(@"if(old!=default){ return "); //初始化目标,区分实体类和接口 if (!info.DeclaringType.IsInterface) { scriptBuilder.AppendLine($"new {info.DeclaringTypeName}"); } scriptBuilder.AppendLine("(old.Select(item=>KeyValuePair.Create("); //克隆Key var keyType = info.DeclaringType.GetGenericArguments()[0]; if (keyType.IsSimpleType()) { scriptBuilder.Append($"item.Key,"); } else { scriptBuilder.AppendLine($"CloneOperator.Clone(item.Key),"); } //克隆Value var valueType = info.DeclaringType.GetGenericArguments()[1]; if (valueType.IsSimpleType()) { scriptBuilder.Append($"item.Value"); } else { scriptBuilder.AppendLine($"CloneOperator.Clone(item.Value)"); } //补全括号,返回默认值。 scriptBuilder.AppendLine(")));}return default;"); var actionBuilder = FastMethodOperator.New; var action = actionBuilder .Using("DeepClone") .Using("System.Linq") .Using(typeof(IDictionary)) .Using(typeof(KeyValuePair <,>)) .Param(info.DeclaringType, "old") .MethodBody(scriptBuilder.ToString()) .Return(info.DeclaringType) .Complie(); return(action); }
public Delegate TypeRouter(NBuildInfo info) { foreach (var item in _handlers) { if (item.MatchType(info.CurrentType)) { return(item.TypeRouter(info)); } } return(default);
public Delegate TypeRouter(NBuildInfo info) { string methodBody; // 简单类型 // SimpleType if (info.ArrayBaseType.IsSimpleType()) { methodBody = GenerateSimpleTypeClone(info); } else if (info.ArrayBaseType == typeof(object)) { // 多维+复杂 // MultiD+Complex if (info.ArrayDimensions > 1) { methodBody = GenerateMultiDWithObjectClone(info); } // 1维+复杂 及 锯齿+复杂 // 1D+Complex and Jagged+Complex else { methodBody = GenerateJaggedWithObjectClone2(info); } } else { // 多维+复杂 // MultiD+Complex if (info.ArrayDimensions > 1) { methodBody = GenerateMultiDWithComplexClone(info); } // 1维+复杂 及 锯齿+复杂 // 1D+Complex and Jagged+Complex else { methodBody = GenerateJaggedWithComplexClone2(info); } } var action = FastMethodOperator.Create(info.CurrentType.GetDomain()) .Using("DeepClone") .Using(typeof(Array)) .Param(info.FatherType, "oldIns") .MethodBody(methodBody) .Return(info.FatherType) .Complie(); return(action); }
public Delegate TypeRouter(NBuildInfo info) { HashSet <string> sets = new HashSet <string>(); CtorHandler = new CloneCtorTempalte(info.DeclaringType); var builder = FastMethodOperator.New; //构造函数处理: 不存在public无参构造函数无法克隆; if (info.DeclaringType.GetConstructor(new Type[0]) == null) { return(default);
/// <summary> /// 1维+复杂 及 锯齿+复杂 /// 1D+Complex 及 Jagged+Complex /// </summary> /// <param name="info"></param> /// <returns></returns> private string GenerateJaggedWithComplexClone2(NBuildInfo info) { var methodBody = $@" if(oldIns==default) return default; {info.DeclaringTypeName} newIns = ({info.DeclaringTypeName})Array.CreateInstance( typeof({info.ElementTypeName}) , oldIns.Length ); for (int i = 0; i < newIns.Length; i++) newIns[i] = CloneOperator.Clone(oldIns[i]); return newIns; "; return(methodBody); }
public Delegate TypeRouter(NBuildInfo info) { if (info.CurrentType.IsGenericType) { StringBuilder scriptBuilder = new StringBuilder(); scriptBuilder.AppendLine(@"if(old!=default){ return "); // 初始化目标,区分实体类和接口 if (!info.CurrentType.IsInterface) { scriptBuilder.AppendLine($"new {info.CurrentTypeName}"); } scriptBuilder.AppendLine("(old.Select(item=>"); var parameters = info.CurrentType.GetGenericArguments(); if (parameters[0].IsSimpleType()) { scriptBuilder.Append("item"); } else if (parameters[0] == typeof(object)) { scriptBuilder.Append("ObjectCloneOperator.Clone(item)"); } else { scriptBuilder.Append("CloneOperator.Clone(item)"); } scriptBuilder.Append("));"); scriptBuilder.AppendLine(@"}return default;"); var action = FastMethodOperator.UseDomain(info.CurrentType.GetDomain()) .Using("DeepClone") .Using("System.Linq") .Param(info.FatherType, "old") .Body(scriptBuilder.ToString()) .Return(info.FatherType) .Compile(); return(action); } else { throw new Exception("暂不支持"); } }
public override bool Condition(NBuildInfo srcMember, NBuildInfo destMember) { return((!srcMember.MemberType.IsImplementFrom(destMember.MemberType)) && ( destMember.MemberType != srcMember.MemberType ) && ( destMember.MemberType.IsImplementFrom(typeof(IEnumerable)) && !destMember.MemberType.IsImplementFrom(typeof(IDictionary)) ) && ( srcMember.MemberType.IsImplementFrom(typeof(IEnumerable)) && !srcMember.MemberType.IsImplementFrom(typeof(IDictionary)) )); }
public Delegate TypeRouter(NBuildInfo info) { var instanceName = "oldSource"; info.FatherType = info.FatherType == typeof(object) ? info.CurrentType : info.FatherType; if (info.CurrentType != info.FatherType) { instanceName = "old"; } CtorHandler = new CtorTempalte(info.CurrentType, instanceName); StringBuilder scriptBuilder = new StringBuilder(); var memberBuilder = new StringBuilder(); var builder = FastMethodOperator.UseDomain(info.CurrentType.GetDomain()); //构造函数处理: 不存在public无参构造函数无法克隆; if (info.CurrentType.GetConstructor(new Type[0]) == null) { return(default);
/// <summary> /// 简单类型 /// SimpleType /// </summary> /// <param name="info"></param> /// <returns></returns> private string GenerateSimpleTypeClone(NBuildInfo info) { var sb = new StringBuilder(); // 多维 & 1维 // MultiD & 1D if (info.ArrayDimensions >= 1 && info.ArrayLayer == 1) { // 生成多维数组结构 // Generate a multi array structure var multiArrLenArr = new string[info.ArrayDimensions]; for (int i = 0; i < info.ArrayDimensions; i++) { multiArrLenArr[i] = $"oldIns.GetLength({i})"; } var multiArrTypeStr = string.Join(",", multiArrLenArr); sb.AppendLine($@" if(oldIns==default) return default; {info.DeclaringTypeName} newIns = new {info.ElementTypeName}[{multiArrTypeStr}]; Array.Copy(oldIns, newIns, newIns.Length); return newIns; "); } // 锯齿 // Jagged else { // 生成锯齿数组结构 // Generating jagged array structure var arrItem = Enumerable.Repeat("[]", info.ArrayLayer - 1); sb.AppendLine($@" if(oldIns==default) return default; {info.DeclaringTypeName} newIns = new {info.ArrayBaseTypeName}[oldIns.Length]{string.Join(string.Empty, arrItem)}; Array.Copy(oldIns, newIns, newIns.Length); return newIns; "); } return(sb.ToString()); }
public Delegate TypeRouter(NBuildInfo info) { var sb = new StringBuilder(); if (info.ArrayBaseType.IsSimpleType()) { sb.AppendLine($@" var newIns = new {info.ElementTypeName}[oldIns.Length]; Array.Copy(oldIns, newIns, newIns.Length); return newIns;"); } else if (info.ArrayDimensions > 1) // 多维数组 { // 循环变量数组 var varNameArr = new string[info.ArrayDimensions]; // 多维数组长度数组 var multiArrLenArr = new string[info.ArrayDimensions]; for (int i = 0; i < info.ArrayDimensions; i++) { var varName = $"_{i}"; varNameArr[i] = varName; multiArrLenArr[i] = $"oldIns.GetLength({i})"; sb.AppendLine($"for (int {varName} = 0; {varName} < oldIns.GetLength({i}); {varName}++)"); } var varNameStr = string.Join(",", varNameArr); sb.AppendLine($@"newIns[{varNameStr}] = CloneOperator.Clone(oldIns[{varNameStr}]);return newIns;"); var multiArrTypeStr = string.Join(",", multiArrLenArr); sb.Insert(0, $"{info.DeclaringType} newIns = new {info.ElementTypeName}[{multiArrTypeStr}];"); } else // 1维数组 || 多维锯齿数组 { sb.AppendLine($@" {info.DeclaringTypeName} newIns = ({info.DeclaringTypeName})Array.CreateInstance( typeof({info.ElementTypeName}) , oldIns.Length ); for (int i = 0; i < newIns.Length; i++) newIns[i] = CloneOperator.Clone(oldIns[i]); return newIns; "); } var tempBuilder = FastMethodOperator.New; var action = tempBuilder .Using("DeepClone") .Using("Natasha") .Using(typeof(Array)) .OopName($"DeepClone{Guid.NewGuid().ToString().Replace("-", string.Empty)}") .MethodName("Clone") .Param(info.DeclaringType, "oldIns") .MethodBody(sb.ToString()) .Return(info.DeclaringType) .Complie(); return(action); }
public override bool Condition(NBuildInfo srcMember, NBuildInfo destMember) { return(srcMember.MemberType != destMember.MemberType && (srcMember.MemberType.IsPrimitive || srcMember.MemberType == typeof(string)) && (destMember.MemberType.IsPrimitive || destMember.MemberType == typeof(string))); }
public override bool Condition(NBuildInfo srcMember, NBuildInfo destMember) { return srcMember.MemberType == destMember.MemberType; }
public override (ScriptConnectionType Type, string Script) Handler(NBuildInfo srcMember, NBuildInfo destMember) { return(ScriptConnectionType.NoAssignment, CollectionScript(srcMember.MemberType, $"arg.{srcMember.MemberName}", destMember.MemberType, $"dest.{destMember.MemberName}")); }
public static Type InitType(Type type, FindTreeType kind = FindTreeType.Hash) { bool isStatic = (type.IsSealed && type.IsAbstract); Type callType = typeof(DictBase); StringBuilder body = new StringBuilder(); var cache = NBuildInfo.GetInfos(type); var setByObjectCache = new Dictionary <string, string>(); var getByObjectCache = new Dictionary <string, string>(); var getByStrongTypeCache = new Dictionary <string, string>(); foreach (var item in cache) { var info = item.Value; string caller = "Instance"; if (info != null) { if (info.IsStatic) { caller = type.GetDevelopName(); } if (info.CanWrite) { setByObjectCache[info.MemberName] = $"{caller}.{info.MemberName} = ({info.MemberTypeName})value;"; } if (info.CanRead) { getByObjectCache[info.MemberName] = $"return {caller}.{info.MemberName};"; getByStrongTypeCache[info.MemberName] = $"return (T)(object)({caller}.{info.MemberName});"; } } } string setObjectBody = default; string getObjectBody = default; string getStrongTypeBody = default; switch (kind) { case FindTreeType.Fuzzy: setObjectBody = BTFTemplate.GetFuzzyPointBTFScript(setByObjectCache, "name"); getObjectBody = BTFTemplate.GetFuzzyPointBTFScript(getByObjectCache, "name"); getStrongTypeBody = BTFTemplate.GetFuzzyPointBTFScript(getByStrongTypeCache, "name"); break; case FindTreeType.Hash: setObjectBody = BTFTemplate.GetHashBTFScript(setByObjectCache, "name"); getObjectBody = BTFTemplate.GetHashBTFScript(getByObjectCache, "name"); getStrongTypeBody = BTFTemplate.GetHashBTFScript(getByStrongTypeCache, "name"); break; case FindTreeType.Precision: setObjectBody = BTFTemplate.GetGroupPrecisionPointBTFScript(setByObjectCache, "name"); getObjectBody = BTFTemplate.GetGroupPrecisionPointBTFScript(getByObjectCache, "name"); getStrongTypeBody = BTFTemplate.GetGroupPrecisionPointBTFScript(getByStrongTypeCache, "name"); break; default: break; } body.AppendLine("public unsafe override void Set(string name,object value){"); body.AppendLine(setObjectBody); body.Append('}'); body.AppendLine("public unsafe override T Get<T>(string name){"); body.AppendLine(getStrongTypeBody); body.Append("return default;}"); body.AppendLine("public unsafe override object GetObject(string name){"); body.AppendLine(getObjectBody); body.Append("return default;}"); if (!isStatic) { callType = typeof(DictBase <>).With(type); body.Append($@"public override void New(){{ Instance = new {type.GetDevelopName()}();}}"); } Type tempClass = NClass.UseDomain(type.GetDomain()) .Public() .Using(type) .Using("System") .Using("NCaller") .UseRandomName() .Namespace("NCallerDynamic") .Inheritance(callType) .Body(body.ToString()) .GetType(); return(tempClass); }
public override (ScriptConnectionType Type, string Script) Handler(NBuildInfo srcMember, NBuildInfo destMember) { return(ScriptConnectionType.NeedAssignment, $"arg.{srcMember.MemberName}"); }
public override bool Condition(NBuildInfo srcMember, NBuildInfo destMember) { return(srcMember.MemberName.ToLower() == destMember.MemberName.ToLower()); }
/// <summary> /// 生成脚本 /// </summary> /// <param name="srcMember">源对象属性信息</param> /// <param name="destMember">生成对象的属性信息</param> /// <returns></returns> public virtual (ScriptConnectionType Type, string Script) Handler(NBuildInfo srcMember, NBuildInfo destMember) { return(default);
public override bool Condition(NBuildInfo srcMember, NBuildInfo destMember) { return(srcMember.MemberType.IsImplementFrom(destMember.MemberType) || srcMember.MemberType.IsSubclassOf(destMember.MemberType)); }
public override (ScriptConnectionType Type, string Script) Handler(NBuildInfo srcMember, NBuildInfo destMember) { var srcMemberScript = $"arg.{srcMember.MemberName}"; var destMemberScrtpt = $"dest.{destMember.MemberName}"; var destCacheKey = $"-dict-{destMember.MemberName}-key"; var destCacheValue = $"-dict-{destMember.MemberName}-value"; StringBuilder builder = new StringBuilder(); builder.AppendLine($@"if({srcMemberScript} != default){{"); if (destMember.MemberType.IsImplementFrom <IDictionary>()) { if (destMember.MemberType.IsInterface) { var destTypes = destMember.MemberType.GetGenericArguments(); var destKeyType = destTypes[0]; var destValueType = destTypes[1]; if (srcMember.MemberType == typeof(IDictionary)) { var tempVarName = $"@temp_dict_{destMember.MemberName}"; if (!destMember.MemberType.IsInterface) { //IDictionary -> Dictionary<key,value> builder.AppendLine($@"var {tempVarName}= {srcMemberScript}.Keys; {destMemberScrtpt} = new {destMember.MemberTypeName}(); foreach(var item in {tempVarName}){{"); var key = MappingCache.ContainsKey(destCacheKey) ? default : $"({ destKeyType.GetDevelopName()})item"; var value = MappingCache.ContainsKey(destCacheValue) ? default : $"({destValueType.GetDevelopName()})({srcMemberScript}[item])"; builder.AppendLine($@"{destMemberScrtpt}[{key}] = {value};"); builder.AppendLine("}"); } else { //IDictionary -> IDictionary<key,value> builder.AppendLine($@"var {tempVarName} = {srcMemberScript}.Keys; {destMemberScrtpt} = new Dictionary<{destKeyType.GetDevelopName()},{destValueType.GetDevelopName()}>(); foreach(var item in {tempVarName}){{"); var key = MappingCache.ContainsKey(destCacheKey) ? default : $"({ destKeyType.GetDevelopName()})item"; var value = MappingCache.ContainsKey(destCacheValue) ? default : $"({destValueType.GetDevelopName()})({srcMemberScript}[item])"; builder.AppendLine($@"{destMemberScrtpt}[{key}] = {value};"); builder.AppendLine("}"); } } else { //IDictionary<key1,value1> -> IDictionary<key,value> builder.Append(destMemberScrtpt + " = "); if (!destMember.MemberType.IsInterface) { builder.Append($"new {destMember.MemberTypeName}"); } builder.Append($"({srcMemberScript}.Select(item=>KeyValuePair.Create("); var srcTypes = srcMember.MemberType.GetGenericArguments(); var srcKeyType = srcTypes[0]; var srctValueType = srcTypes[1]; if (destKeyType != srcKeyType) { if (MappingCache.ContainsKey(destCacheKey)) { builder.Append(MappingCache[destCacheKey]); } else if ( (destKeyType.IsPrimitive || destKeyType == typeof(string)) && (srcKeyType.IsPrimitive || srcKeyType == typeof(string))) { builder.Append($"Convert.To{destKeyType.Name}(item.Key)"); } else { builder.Append($"MapperOperator<{destKeyType.GetDevelopName()}>.MapFrom(item.Key)"); } } else { builder.Append($"item.Key"); } if (destValueType != srctValueType) { if (MappingCache.ContainsKey(destCacheValue)) { // key = Key.Name builder.Append(MappingCache[destCacheValue]); } else if ( (destValueType.IsPrimitive || destValueType == typeof(string)) && (srctValueType.IsPrimitive || srctValueType == typeof(string))) { // key = Convert.Int32(Key) builder.Append($"Convert.To{destValueType.Name}(item.Value)"); } else { // Key = MapperOperator<T>.MapFrom(Key); builder.Append($"MapperOperator<{destValueType.GetDevelopName()}>.MapFrom(item.Value)"); } } else { builder.Append($"item.Value"); } builder.Append(')'); } } } else { var destTypes = destMember.MemberType.GetGenericArguments(); var destKeyType = destTypes[0]; var destValueType = destTypes[1]; if (destMember.MemberType.IsInterface) { if (srcMember.MemberType == typeof(IDictionary) || (destMember.MemberType == typeof(IEnumerable) || destMember.MemberType == typeof(ICollection))) { //IDictionary -> ICollection / IEnumable if (MappingCache.ContainsKey(destCacheKey)) { var tempScript = MappingCache[destCacheKey]; if (tempScript == default) { builder.Append($"{destMemberScrtpt} = {srcMemberScript}.Keys;"); } else { builder.Append($"{destMemberScrtpt} = {tempScript};"); } } else if (MappingCache.ContainsKey(destCacheValue)) { var tempScript = MappingCache[destCacheValue]; if (tempScript == default) { builder.Append($"{destMemberScrtpt} = {srcMemberScript}.Values;"); } else { builder.Append($"{destMemberScrtpt} = {tempScript};"); } } else { builder.Append($"{destMemberScrtpt} = {srcMemberScript}.Values;"); } } else { //IDictionary<k,v> -> ICollection / IEnumable if (MappingCache.ContainsKey(destCacheKey)) { var tempScript = MappingCache[destCacheKey]; if (tempScript == default) { builder.Append($"{destMemberScrtpt} = {srcMemberScript}.Keys;"); } else { builder.Append($"{destMemberScrtpt} = {tempScript};"); } } else if (MappingCache.ContainsKey(destCacheValue)) { var tempScript = MappingCache[destCacheValue]; if (tempScript == default) { builder.Append($"{destMemberScrtpt} = {srcMemberScript}.Values;"); } else { builder.Append($"{destMemberScrtpt} = {tempScript};"); } } else { builder.Append($"{destMemberScrtpt} = {srcMemberScript}.Values;"); } } } //Dict -> ICollection if (!destMember.MemberType.IsInterface) { if (MappingCache.ContainsKey(destCacheKey)) { } } else if (MappingCache.ContainsKey(destCacheValue)) { } //IDictionary -> Dictionary<key,value> builder.AppendLine($@"var temp{destMember.MemberName} = {srcMemberScript}.Keys; dest.{destMember.MemberName} = new {destMember.MemberTypeName}(); foreach(var item in temp{destMember.MemberName}){{"); var key = MappingCache.ContainsKey($"-dict-{destMember.MemberName}-key") ? default : $"({ destKeyType.GetDevelopName()})item"; var value = MappingCache.ContainsKey($"-dict-{destMember.MemberName}-value") ? default : $"({destValueType.GetDevelopName()})({srcMemberScript}[item])"; builder.AppendLine($@"dest.{destMember.MemberName}[{key}] = {value};"); builder.AppendLine("}"); }
public Delegate TypeRouter(NBuildInfo info) { //构造函数处理: 不存在public无参构造函数无法克隆; if (info.DeclaringType.GetConstructor(new Type[0]) == null) { return(default);
/// <summary> /// 检测条件 /// </summary> /// <param name="srcMember">源对象属性信息</param> /// <param name="destMember">生成对象的属性信息</param> /// <returns></returns> public virtual bool Condition(NBuildInfo srcMember, NBuildInfo destMember) { return(false); }