Пример #1
0
 public override string ScriptKeyAction(IDictionary <TKey, string> dict)
 {
     return(BTFTemplate.GetCustomerBTFScript(dict, _keySwitchCode, _keyCaseCode));
 }
Пример #2
0
 public override string ScriptValueAction(IDictionary <TValue, string> dict)
 {
     return(BTFTemplate.GetHashBTFScript(dict));
 }
            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());
            }
Пример #4
0
        public static Type InitType(Type type, FindTreeType kind = FindTreeType.Hash)
        {
            bool isStatic = (type.IsSealed && type.IsAbstract);
            Type callType = typeof(LinkBase);


            StringBuilder body = new StringBuilder();


            var cache                = NBuildInfo.GetInfos(type);
            var setByObjectCache     = new Dictionary <string, string>();
            var getIndexBodyCache    = new Dictionary <string, string>();
            var getByStrongTypeCache = new Dictionary <string, string>();
            var getLinkBaseBodyCache = 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.CanRead)
                    {
                        getIndexBodyCache[info.MemberName]    = $"return (T)(object)({caller}.{info.MemberName});";
                        getByStrongTypeCache[info.MemberName] = $"return (T)(object)({caller}.{info.MemberName});";
                        getLinkBaseBodyCache[info.MemberName] = $"return {caller}.{info.MemberName}.LinkCaller();";
                    }


                    if (info.CanWrite)
                    {
                        setByObjectCache[info.MemberName] = $"{caller}.{info.MemberName} = ({info.MemberTypeName})value;";
                    }
                }
            }


            string setObjectBody     = default;
            string getStrongTypeBody = default;
            string setIndexBody      = default;
            string getIndexBody      = default;
            string getLinkBaseBody   = default;


            switch (kind)
            {
            case FindTreeType.Fuzzy:
                setObjectBody     = BTFTemplate.GetFuzzyPointBTFScript(setByObjectCache, "name");
                setIndexBody      = BTFTemplate.GetFuzzyPointBTFScript(setByObjectCache, "_name");
                getIndexBody      = BTFTemplate.GetFuzzyPointBTFScript(getIndexBodyCache, "_name");
                getStrongTypeBody = BTFTemplate.GetFuzzyPointBTFScript(getByStrongTypeCache, "name");
                getLinkBaseBody   = BTFTemplate.GetFuzzyPointBTFScript(getLinkBaseBodyCache, "name");
                break;

            case FindTreeType.Hash:
                setObjectBody     = BTFTemplate.GetHashBTFScript(setByObjectCache, "name");
                setIndexBody      = BTFTemplate.GetHashBTFScript(setByObjectCache, "_name");
                getIndexBody      = BTFTemplate.GetHashBTFScript(getIndexBodyCache, "_name");
                getStrongTypeBody = BTFTemplate.GetHashBTFScript(getByStrongTypeCache, "name");
                getLinkBaseBody   = BTFTemplate.GetHashBTFScript(getLinkBaseBodyCache, "name");
                break;

            case FindTreeType.Precision:
                setObjectBody     = BTFTemplate.GetGroupPrecisionPointBTFScript(setByObjectCache, "name");
                setIndexBody      = BTFTemplate.GetGroupPrecisionPointBTFScript(setByObjectCache, "_name");
                getIndexBody      = BTFTemplate.GetGroupPrecisionPointBTFScript(getIndexBodyCache, "_name");
                getStrongTypeBody = BTFTemplate.GetGroupPrecisionPointBTFScript(getByStrongTypeCache, "name");
                getLinkBaseBody   = BTFTemplate.GetGroupPrecisionPointBTFScript(getLinkBaseBodyCache, "name");
                break;

            default:
                break;
            }


            body.AppendLine("public unsafe override LinkBase Get(string name){");
            body.AppendLine(getLinkBaseBody);
            body.Append("return default;}");



            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 T Get<T>(){");
            body.AppendLine(getIndexBody);
            body.Append("return default;}");


            body.AppendLine("public unsafe override void Set(object value){");
            body.AppendLine(setIndexBody);
            body.Append("}");


            if (!isStatic)
            {
                callType = typeof(LinkBase <>).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()
                             .Inheritance(callType)
                             .Namespace("NCallerDynamic")
                             .Body(body.ToString())
                             .GetType();


            return(tempClass);
        }
Пример #5
0
 public static FDC <P, V> operator |(IDictionary <string, string> dict, FDC <P, V> template)
 {
     template.FindContent = BTFTemplate.GetFuzzyPointBTFScript(dict);
     return(template);
 }