简体   繁体   English

创建一个名称包含在字符串中的类的实例

[英]Creating an instance of a class with a name held in a string

I'm looking for a way to initialize a new instance of a class with a string as its name in order for me to find that specific instance in a list at a later point. 我正在寻找一种以字符串作为名称来初始化类的新实例的方法,以便稍后在列表中找到该特定实例。

Currently I have something along the lines of this code: 目前,我有一些与此代码类似的东西:

static List<ClassItem> classList = new List<ClassItem>();

private void updateClassList(Stream stream)
{
    Message = Recieve(stream);
    Interact(Message); //get the number of Classes about to be recieved

    string ID;
    string state;

    for(int i = 0; i < numberOfClasses; i++)
    {
        Message = Recieve(stream);
        interpretClassProperties(Message, out ID, out State);

        ClassItem ID = new ClassItem(ID, state); //incorrect code
        classList.Add(ID); //add new instance to list
    }
}

Obviously this isn't going to work as I can't use a variable to initialise a class instance, however logically it shows what I want to achieve. 显然,这是行不通的,因为我不能使用变量来初始化类实例,但是从逻辑上讲,它显示了我想要实现的目标。 Each loop will add an instance of ClassItem (with the appropriate ID value as a name) to the classList so I can find it later. 每个循环都会将ClassItem的实例(具有适当的ID值作为名称)添加到classList以便稍后找到它。

What should I look into in order to achieve this? 为了实现这一目标,我应该考虑什么?

Any feedback appreciated, including any warnings of future problems I may have in approaching the problem in this fashion. 感谢您提供任何反馈意见,包括在以这种方式解决问题时可能遇到的未来问题的任何警告。 (Ie finding a class instance in a List by name). (即按名称在List中查找类实例)。

Use Activator.CreateInstance: 使用Activator.CreateInstance:

public static ObjectHandle CreateInstance(
    string assemblyName,
    string typeName
)

You know your assembly name and receive the class name (type name). 您知道您的程序集名称,并收到类名称(类型名称)。

MSDN: https://msdn.microsoft.com/en-us/library/d133hta4(v=vs.110).aspx MSDN: https//msdn.microsoft.com/zh-CN/library/d133hta4(v = vs.110) .aspx

Sample code: 样例代码:

static List<object> classList = new List<object>();

private void updateClassList(Stream stream)

    {
        Message = Recieve(stream);
        Interact(Message); //get the number of Classes about to be recieved

        string id;

        for(int i = 0; i < numberOfClasses; i++)
        {
            Message = Recieve(stream);
            interpretClassProperties(Message, out id);

            classList.Add(Activator.CreateInstance("AssemblyName", id).Unwrap());
        }
    }

Is something like this what you're looking for? 您正在寻找类似这样的东西吗? Beware, though, this will likely create a memory nightmare in time unless you are certain to dereference your instances from the static list. 但是请注意,除非您确定要从静态列表中取消引用实例,否则这可能会及时造成内存噩梦。

public class ClassWithIds
{
    public static List<ClassWithIds> Instances = new List<ClassWithIds>();

    private static int _idSeed = 0;

    private readonly string _name;

    public string Name
    {
        get
        {
            return _name;
        }
    }

    private static int NextId()
    {
        return Interlocked.Increment(ref _idSeed);
    }

    public ClassWithIds()
    {
        _name = this.GetType().FullName + " Number " + NextId();
        Instances.Add(this);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM