public static unsafe DictBase Ctor(Type type) { //获得动态生成的类型 var proxyType = DictBuilder.InitType(type, AlgorithmKind.Fuzzy); //加入缓存 var script = $"return new {proxyType.GetDevelopName()}();"; _str_cache[type.GetDevelopName()] = script; _type_cache[type] = script; var newFindTree = "var str = arg.GetDevelopName();"; newFindTree += BTFTemplate.GetGroupPrecisionPointBTFScript(_str_cache, "str"); newFindTree += $"return PrecisionDictBuilder.Ctor(arg);"; //生成脚本 var newAction = NDelegate .UseDomain(type.GetDomain(), builder => builder.LogCompilerError()) .UnsafeFunc <Type, DictBase>(newFindTree, _type_cache.Keys.ToArray(), "NMS.Leo.NCallerDynamic"); HashDictOperator.CreateFromString = (delegate * managed <Type, DictBase>)(newAction.Method.MethodHandle.GetFunctionPointer()); return((DictBase)Activator.CreateInstance(proxyType)); }
public void GroupsPrecisionFindTree() { PrecisionDelegate = NDomain.Random().UnsafeFunc <string, int>(BTFTemplate.GetGroupPrecisionPointBTFScript(ScriptDict) + "return default;"); //var temp = BTFTemplate.GetPrecisionPointBTFScript(ScriptDict) + "return default;"; foreach (var item in Dict) { Assert.Equal(item.Value, PrecisionDelegate(item.Key)); } }
static HttpContextUpdateOperator() { var stringBuilder = new StringBuilder(); var propNames = typeof(TEntity).GetProperties().Select(item => item.Name); var allowList = PropertiesCache <TEntity> .GetAllowUpdateFields(); stringBuilder.AppendLine($"Expression<Func<{typeof(TEntity).GetDevelopName()},bool>> exp = default;"); Dictionary <string, string> dict = new Dictionary <string, string>(); foreach (var name in propNames) { if (allowList.Contains(name)) { dict[name] = $"exp = obj => obj.{name} == arg2.{name};"; } } stringBuilder.AppendLine(BTFTemplate.GetGroupPrecisionPointBTFScript(dict, "arg1")); stringBuilder.AppendLine("return exp;"); UpdateFieldsHandler += NDelegate .DefaultDomain() .UnsafeFunc <string, TEntity, Expression <Func <TEntity, bool> > >(stringBuilder.ToString()); }
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 string ScriptKeyAction(IDictionary <string, string> dict, string paramName) { return(BTFTemplate.GetGroupPrecisionPointBTFScript(dict, paramName)); }
public static PDC <P, V> operator |(IDictionary <string, string> dict, PDC <P, V> template) { template.FindContent = BTFTemplate.GetGroupPrecisionPointBTFScript(dict); return(template); }
public static Type InitType(Type type, AlgorithmKind kind = AlgorithmKind.Hash) { var isStatic = type.IsSealed && type.IsAbstract; var callType = typeof(DictBase); var body = new StringBuilder(); var setByObjectCache = new Dictionary <string, string>(); var getByObjectCache = new Dictionary <string, string>(); var getByStrongTypeCache = new Dictionary <string, string>(); var getByLeoMembersCache = new Dictionary <string, LeoMember>(); var getByLeoMembersScriptCache = new Dictionary <string, string>(); var getByReadOnlyStaticScriptBuilder = new StringBuilder(); var getByReadOnlySettingScriptBuilder = new StringBuilder(); var getByInternalNamesScriptBuilder = new StringBuilder(); #region Field var fields = type.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (var field in fields) { if (field.IsSpecialName || field.Name.Contains("<")) { continue; } var caller = "Instance"; if (field.IsStatic) { caller = type.GetDevelopName(); } var fieldName = field.Name; var fieldType = field.FieldType.GetDevelopName(); //set if (!field.IsLiteral) { var fieldScript = $"{caller}.{fieldName}"; if (field.IsInitOnly) { fieldScript = fieldScript.ReadonlyScript(); } setByObjectCache[fieldName] = $"{fieldScript} = ({fieldType})value;"; } //get getByObjectCache[fieldName] = $"return {caller}.{fieldName};"; getByStrongTypeCache[fieldName] = $"return (T)(object)({caller}.{fieldName});"; //member metadata getByLeoMembersCache[fieldName] = field; getByLeoMembersScriptCache[fieldName] = $"return __metadata_LeoMember_{fieldName};"; getByReadOnlyStaticScriptBuilder.AppendLine($@"private static readonly LeoMember __metadata_LeoMember_{fieldName};"); getByInternalNamesScriptBuilder.Append($@"""{fieldName}"","); getByReadOnlySettingScriptBuilder.Append($"__metadata_LeoMember_{fieldName}".ReadonlyScript()); getByReadOnlySettingScriptBuilder.Append($@" = leoMembersCache[""{fieldName}""];"); } #endregion #region Property var props = type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (var property in props) { var method = property.CanRead ? property.GetGetMethod(true) : property.GetSetMethod(true); var caller = "Instance"; if (method.IsStatic) { caller = type.GetDevelopName(); } var propertyName = property.Name; var propertyType = property.PropertyType.GetDevelopName(); var propertyScript = $"{caller}.{propertyName}"; //set if (property.CanWrite) { setByObjectCache[propertyName] = $"{propertyScript} = ({propertyType})value;"; } //get if (property.CanRead) { getByObjectCache[propertyName] = $"return {caller}.{propertyName};"; getByStrongTypeCache[propertyName] = $"return (T)(object)({caller}.{propertyName});"; } //member metadata getByLeoMembersCache[propertyName] = property; getByLeoMembersScriptCache[propertyName] = $"return __metadata_LeoMember_{propertyName};"; getByReadOnlyStaticScriptBuilder.AppendLine($@"private static readonly LeoMember __metadata_LeoMember_{propertyName};"); getByInternalNamesScriptBuilder.Append($@"""{propertyName}"","); getByReadOnlySettingScriptBuilder.Append($"__metadata_LeoMember_{propertyName}".ReadonlyScript()); getByReadOnlySettingScriptBuilder.Append($@" = leoMembersCache[""{propertyName}""];"); } #endregion string setObjectBody = default; string getObjectBody = default; string getStrongTypeBody = default; string getLeoMemberBody = default; switch (kind) { case AlgorithmKind.Fuzzy: setObjectBody = BTFTemplate.GetGroupFuzzyPointBTFScript(setByObjectCache, "name"); getObjectBody = BTFTemplate.GetGroupFuzzyPointBTFScript(getByObjectCache, "name"); getStrongTypeBody = BTFTemplate.GetGroupFuzzyPointBTFScript(getByStrongTypeCache, "name"); getLeoMemberBody = BTFTemplate.GetGroupFuzzyPointBTFScript(getByLeoMembersScriptCache, "name"); break; case AlgorithmKind.Hash: setObjectBody = BTFTemplate.GetHashBTFScript(setByObjectCache, "name"); getObjectBody = BTFTemplate.GetHashBTFScript(getByObjectCache, "name"); getStrongTypeBody = BTFTemplate.GetHashBTFScript(getByStrongTypeCache, "name"); getLeoMemberBody = BTFTemplate.GetHashBTFScript(getByLeoMembersScriptCache, "name"); break; case AlgorithmKind.Precision: setObjectBody = BTFTemplate.GetGroupPrecisionPointBTFScript(setByObjectCache, "name"); getObjectBody = BTFTemplate.GetGroupPrecisionPointBTFScript(getByObjectCache, "name"); getStrongTypeBody = BTFTemplate.GetGroupPrecisionPointBTFScript(getByStrongTypeCache, "name"); getLeoMemberBody = BTFTemplate.GetGroupPrecisionPointBTFScript(getByLeoMembersScriptCache, "name"); break; } //To add readonly metadata (LeoMember) properties. body.AppendLine(getByReadOnlyStaticScriptBuilder.ToString()); body.AppendLine("public unsafe override void Set(string name,object value){"); body.AppendLine(setObjectBody); body.Append('}'); #if NET5_0 body.AppendLine("[SkipLocalsInit]"); #endif body.AppendLine("public unsafe override T Get<T>(string name){"); body.AppendLine(getStrongTypeBody); body.Append("return default;}"); #if NET5_0 body.AppendLine("[SkipLocalsInit]"); #endif body.AppendLine("public unsafe override object GetObject(string name){"); body.AppendLine(getObjectBody); body.Append("return default;}"); body.AppendLine("public unsafe override LeoMember GetMember(string name){"); body.AppendLine(getLeoMemberBody); body.Append("return default;}"); body.AppendLine("protected override HashSet<string> InternalMemberNames { get; } = new HashSet<string>(){"); body.AppendLine(getByInternalNamesScriptBuilder.ToString()); body.Append("};"); body.AppendLine("public static void InitMetadataMapping(Dictionary<string, LeoMember> leoMembersCache){"); body.AppendLine(getByReadOnlySettingScriptBuilder.ToString()); body.Append('}'); if (!isStatic) { callType = typeof(DictBase <>).With(type); body.Append($@"public override void New(){{ Instance = new {type.GetDevelopName()}();}}"); } else { body.Append($@"public override void SetObjInstance(object obj){{ }}"); } var tempClass = NClass.UseDomain(type.GetDomain()) .Public() .Using(type) .AllowPrivate(type.Assembly) .Namespace("NMS.Leo.NCallerDynamic") .Inheritance(callType) .Body(body.ToString()) .GetType(); InitMetadataMappingCaller(tempClass)(getByLeoMembersCache); return(tempClass); }
private Template(Type type) { _ctors = type.GetConstructors(); _ctorIndexMap = new Dictionary <ConstructorInfo, int>(); _nameConstructorMap = new Dictionary <string, List <(ConstructorInfo, ParameterInfo)> >(); _creators = new Func <object[], object> [_ctors.Length]; _typeFullQualifiedName = type.GetDevelopName(); var maxParametersCount = 0; for (var index = 0; index < _ctors.Length; ++index) { _ctorIndexMap[_ctors[index]] = index; var @params = _ctors[index].GetParameters(); if (@params.Length > maxParametersCount) { maxParametersCount = @params.Length; } if (@params.Length == 0) { _defaultCtorIndex = index; } var args = new ParameterInfo[@params.Length]; for (var jack = 0; jack < @params.Length; ++jack) { args[@params[jack].Position] = @params[jack]; var key = @params[jack].ParameterType.Name + @params[jack].Name; if (!_nameConstructorMap.ContainsKey(key)) { _nameConstructorMap[key] = new List <(ConstructorInfo, ParameterInfo)>(); } _nameConstructorMap[key].Add((_ctors[index], @params[jack])); } var paramsScript = new StringBuilder(); for (var king = 0; king < args.Length; ++king) { paramsScript.AppendLine(@$ "{args[king].Name}: arg[{king}] == null ? default : ({args[king].ParameterType.GetDevelopName()})arg[{king}]"); if (king != args.Length - 1) { paramsScript.Append(","); } } _creators[index] = NDelegate.RandomDomain().Func <object[], object>($"return new {_typeFullQualifiedName}({paramsScript});"); } var dynamicDictionary = new Dictionary <string, string>(); var script = new StringBuilder(); var resultFullQualifiedName = typeof(TypeVisit.CtorMatchedResult).GetDevelopName(); script.Append($@" if(arg == default || arg.Count() == 0) {{ return new {resultFullQualifiedName}(null, {_defaultCtorIndex}); }} int[] index = new int[{_ctors.Length}]; object[][] values = new object[{_ctors.Length}][]; for(var jack = 0; jack < {_ctors.Length}; ++jack) {{ values[jack] = new object[{maxParametersCount}]; }} foreach(var item in arg) {{ string temp = item.Type.Name + item.Name; "); var loopScript = new StringBuilder(); foreach (var item in _nameConstructorMap) { loopScript.Clear(); foreach (var ctor in item.Value) { loopScript.AppendLine($"index[{_ctorIndexMap[ctor.Item1]}]+=1;"); loopScript.AppendLine($"values[{_ctorIndexMap[ctor.Item1]}][{ctor.Item2.Position}] = item.Value;"); } dynamicDictionary[item.Key] = loopScript.ToString(); } loopScript.Clear(); script.AppendLine(BTFTemplate.GetGroupPrecisionPointBTFScript(dynamicDictionary, "temp")); script.AppendLine("}"); script.AppendLine(@$ " int maxValue = 0; int maxIndex = 0; for(var king = 0; king < index.Length; ++king) {{ if(maxValue < index[king]) {{ maxValue = index[king]; maxIndex = king; }} }} return new {resultFullQualifiedName}(values[maxIndex], maxIndex); "); GetIndex = NDelegate.RandomDomain(c => c.SyntaxErrorBehavior = ExceptionBehavior.Throw) .UnsafeFunc <IEnumerable <ArgumentDescriptor>, TypeVisit.CtorMatchedResult>(script.ToString()); }