采猬 2021-06-01 15:09 采纳率: 66.7%
浏览 56
已采纳

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

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

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

  • 写回答

2条回答 默认 最新

  • CSDN专家-文盲老顾 2021-06-01 17:35
    关注
            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 ,用来替换字符串中的特殊符号加转义,比如 .+*[]等
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?