c# 已知字符串内容,如果把字符串作为泛型使用

c# 已知字符串内容,如果把字符串作为泛型使用

问题描述:

var kind ="PageSkill";
var service =new SkillService<PageSkill>();

如何根据已知变量kind,确定第二行代码的泛型?

        public static Type GetTypeByName(string typename)
        {
            Type t = null;
            string source = typename;
            if (source.IndexOf('<') > 0)
            {
                List<string> lv = new List<string>();
                while (RegexExpand.IsMatch(source, @"<[^<>]+>"))
                {
                    lv.Add(RegexExpand.Match(source, @"(?<=<)[^<>]+(?=>)").Value);
                    source = RegexExpand.Replace(source, @"<[^<>]+>", "/" + (lv.Count - 1));
                }
                List<Type[]> args = new List<Type[]>();
                for (int i = 0; i < lv.Count; i++)
                {
                    List<Type> arg = new List<Type>();
                    string[] sp = lv[i].Split(',');
                    for (int j = 0; j < sp.Length; j++)
                    {
                        string s = sp[j].Trim();
                        if (!string.IsNullOrEmpty(s))
                        {
                            if (RegexExpand.IsMatch(s, @"/\d+$"))
                            {
                                Match m = RegexExpand.Match(s, @"^([^/\s]+)\s*/(\d+)$");
                                if (!m.Success)
                                {
                                    throw new Exception("");
                                }
                                Type p = GetTypeByName(m.Groups[1].Value);
                                Type c = p.MakeGenericType(args[Convert.ToInt32(m.Groups[2].Value)]);
                                arg.Add(c);
                            }
                            else
                            {
                                arg.Add(GetTypeByName(s));
                            }
                        }
                    }
                    args.Add(arg.ToArray());
                }
                Match f = RegexExpand.Match(source, @"^([^/\s]+)\s*/(\d+)$");
                if (!f.Success)
                {
                    throw new Exception("");
                }
                Type fp = GetTypeByName(f.Groups[1].Value);
                Type fc = fp.MakeGenericType(args[Convert.ToInt32(f.Groups[2].Value)]);
                return fc;
            }
            else
            {
                try
                {
                    t = Type.GetType(source);
                    if (t != null)
                    {
                        return t;
                    }
                    Assembly[] assembly = AppDomain.CurrentDomain.GetAssemblies();
                    foreach (Assembly ass in assembly)
                    {
                        t = ass.GetType(source);
                        if (t != null)
                        {
                            return t;
                        }
                        Type[] ts = ass.GetTypes();
                        foreach (Type st in ts)
                        {
                            if (RegexExpand.IsMatch(st.FullName, @"\." + RegexExpand.FormatRegExp(source) + @"(`?\d+)?$"))
                            {
                                return st;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {

                }
            }
            return t;
        }

// 调用的时候很简单
                string typename = "Dictionary<Dictionary<string,List<Ajax>>,int>";
                Type t = Common.GetTypeByName(typename);
                dynamic item = t.Assembly.CreateInstance(t.FullName);
// 代码中出现RegexExpand 可用 Regex 代替,代替后记得加忽略大小写的参数
// FormatRegExp ,用来替换字符串中的特殊符号加转义,比如 .+*[]等

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632