简体   繁体   中英

How can i dynamically create an objects of the class?

I have a class MyClass :

public class OPCTag
{
    public string tagName;
    public string tagOvationMatch;
    public string tagValue;
}

I know how to create an object of class manually, but how can i dynamically create and objects of this class ? For example, to create it at the for cycle:

for (int i=0; i< 10; i++)
{
    //CREATE OBJECT DYNAMICALLY
}

To get after it a 10 objects of MyClass.

If you mean simply creating an instance of a class always with the same type, then this will be enough for you:

List<OPCTag> list = new List<OPCTag>();
for (int i = 0; i < 10; i++)
{
    // Create object of OPCTag
    var obj = new OPCTag();

    // Set value for 'tagName' property 
    obj.tagName = "New value of 'tagName'";

    // Get value of 'tagName' property
    var tagNameValue = obj.tagName;

    list.Add(obj);
}

// Set value of 4th element 'tagName' property
list[4].tagName = "This is 4th element";

// Get value of 4th element 'tagName' property
var valueOf4thTag = list[4].tagName;

But if you want to create classes dynamically with unknown types, you should use reflection:

// Type namespace and name
string typeName = typeof(OPCTag).FullName; // MyNamespace.OPCTag

List<Object> list = new List<Object>();
for (int i = 0; i < 10; i++)
{
    // Create object dynamically from string type
    var obj = Type.GetType(typeName).GetConstructor(new Type[0]).Invoke(new object[0]);

    // Set value for 'tagName' property
    obj.GetType().GetProperty("tagName").SetValue(obj, "New value for 'tagName'");

    // Get value from 'tagName' property
    string tagNameValue = (string)obj.GetType().GetProperty("tagName").GetValue(obj);

    list.Add(obj);
}

You can add your objects dynamically in a loop, but you have to create a list. You can write something like that :

List<OPCTag> lstTag = new List<OPCTag>();

for(int i = 0; i<10 ; i++)
{
     lstTag.Add(new OPCTag());
}

To create 10 objects of MyClass you can do, for example:

List<MyClass> list = new List<MyClass>();
for (int i = 0; i < 10; i++)
{
    list.Add(new MyClass());    
}

Though it's not "dynamically creating objects", just creating instances.

For Dictionary you should specify key and value. They can be of any type, not just int (usually string ).

Dictionary<int, MyClass> dictionary = new Dictionary<int, MyClass>();
for (int i = 0; i < 10; i++)
{
    dictionary.Add(i, new MyClass());
}

//Your class

public class OPCTag
{
    public string tagName;
    public string tagOvationMatch;
    public string tagValue;
}

// Initialize new list object of type OPCTag

List<OPCTag> lstTag = new List<OPCTag>();

for(int i = 0; i<10 ; i++)
{
     lstTag.Add(new OPCTag());
}

//To read value from lstTag based on index here I specify the 0 index / first value in list.

var firstIndexTagname =  lstTag[0].tagName;
var firstIndexTagOvationMatch = lstTag[0].tagOvationMatch;
var firstIndexTagValue =  lstTag[0].tagValue;

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