创建实例 [英] Creating instance

查看:82
本文介绍了创建实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建名称以字符串形式给出的实例.标识符的名称应为给定的字符串

表示SomeClass< string提供的="="> = new SomeClass();

我需要中止一些名称由某些正则表达式定义的线程

可以说

t是第一线程
ta被秒杀
taa位居第三
tba出现了
c是第五名
等等...

因此,中止其标识符以ta开头的所有线程.

how to create an instance whose name is given as a string. the name of identifier should b the string that is given

means SomeClass <string provided=""> = new SomeClass();

i need to Abort some threads whose names are defined by some regular expression

lets say

t is 1st thread
ta is secound
taa is third
tba is forth
c is fifth
and so on...

so Abort all thread whose identifier starts from ta.

推荐答案

Reflection可用于执行此操作.我怀疑还有更好的方法,例如创建一个字典以将线程映射到名称.
Reflection can be used to do that. I suspect there''s a much better way, though, like creating a dictionary to map threads to names.


yp字典已经完成了工作
谢谢兄弟

yp Dictionary has donw the work
thanks bro

Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add( "t","thread1");
            dic.Add( "ta","thread2");
            dic.Add( "taa","thread3");
            dic.Add( "tba","thread4");
            dic.Add( "tbb","thread5");
            dic.Add( "c","thread6");
            List<string> k = dic.Keys.ToList<string>();
            for(int i=0 ;i<k.Count;i++)

                if (Regex.IsMatch(k[i], "ta"))

                {

                    dic.Remove(k[i]);

                    Console.WriteLine(k[i]);

                }



这就是我所做的



this is what i did


这篇关于创建实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆