简体   繁体   中英

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.

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).

Use 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

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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