C# 将字符串转换为类

C# 将字符串转换为类,c#,asp.net,C#,Asp.net,我正在尝试将字符串转换为特定的类。字符串是类名。我有一个sql过程,它返回三组数据,即客户机类的主记录。然后是次要的Address类,最后是另一个次要的Contact类 我使用SqlDataReader返回sql过程数据,并使用reader.nextResults()遍历数据 字符串值来自SQL过程。 例如:“地址”或“联系人列表” 公共静态类客户端 { Guid Id{get;set;} 字符串ClientName{get;set;} 地址客户端地址{get;set;} 列出客户端联系人{ge

我正在尝试将字符串转换为特定的类。字符串是类名。我有一个sql过程,它返回三组数据,即客户机类的主记录。然后是次要的Address类,最后是另一个次要的Contact类

我使用SqlDataReader返回sql过程数据,并使用reader.nextResults()遍历数据

字符串值来自SQL过程。 例如:“地址”或“联系人列表”

公共静态类客户端
{
Guid Id{get;set;}
字符串ClientName{get;set;}
地址客户端地址{get;set;}
列出客户端联系人{get;set;}
}
公共静态类地址
{
Guid Id{get;set;}
Guid ClientId{get;set;}
字符串AddressLine1{get;set;}
字符串AddressLine2{get;set;}
字符串地址行3{get;set;}
字符串地址行4{get;set;}
字符串PostalCode{get;set;}
}
公共静态类联系人
{
Guid Id{get;set;}
Guid ClientId{get;set;}
字符串ContactNumber{get;set;}
字符串联系人类型{get;set;}
}
任何帮助都将不胜感激

谢谢。

编辑

我会用反思

Dictionary<string, Type> dict = new Dictionary<string, Type>();
dict.Add("Foo", typeof(Foo));
dict.Add("Bar", typeof(Bar));

object obj = Activator.CreateInstance(dict[className]);

...

public class Foo
{
    public Foo()
    {
        Console.WriteLine("new foo");
    }
}

public class Bar
{
    public Bar()
    {
        Console.WriteLine("new bar");
    }
}

您可以使用
Activator.CreateInstance(“您的汇编名”、“您的类名”)
方法。还有更多信息吗?字符串操作会在一段时间后变得棘手。另外,在您的情况下,它可能不起作用,因为无法链接实际的3个类

看看这种方法是否有意义:

Dictionary<Guid, Client> clients = new Dictionary<Guid, Client>();

// first read all the Client records.
while (reader.Read())
{
 Guid clientId = Guid.Parse(reader["Id"].ToString());
 Client tempClient = new Client();
 tempClient.Contacts = new List<Contact>();
 // construct the tempClient completely using all fields.

 clients.Add(clientId, tempClient);
}

// now move onto the address records.
reader.nextResults();

while (reader.Read())
{
 Guid clientId = Guid.Parse(reader["ClientId"].ToString());
 clients[clientId].Address = new Address();
 // construct the clients[clientId].Address completely using all fields.
}

// now move onto the contact records.
reader.nextResults();

while (reader.Read())
{
 Guid clientId = Guid.Parse(reader["ClientId"].ToString());
 Contact tempContact = new Contact();
 // construct the tempContact completely using all fields.

 clients[clientId].Contacts.Add(tempContact)
}

// at the end, the clients dictionary has all the Client objects linked with the right 
// Address and Contact objects.
Dictionary clients=newdictionary();
//首先读取所有客户机记录。
while(reader.Read())
{
Guid clientId=Guid.Parse(读卡器[“Id”].ToString());
Client tempClient=新客户端();
tempClient.Contacts=新列表();
//使用所有字段完全构建tempClient。
clients.Add(clientId,tempClient);
}
//现在转到地址记录。
reader.nextResults();
while(reader.Read())
{
Guid clientId=Guid.Parse(读卡器[“clientId”].ToString());
客户端[clientId]。地址=新地址();
//使用所有字段完全构造客户端[clientId]。地址。
}
//现在转到联系人记录。
reader.nextResults();
while(reader.Read())
{
Guid clientId=Guid.Parse(读卡器[“clientId”].ToString());
联系人tempContact=新联系人();
//使用所有字段完全构造tempContact。
客户端[clientId].Contacts.Add(临时联系人)
}
//最后,客户端字典将所有客户端对象链接到右侧
//地址和联系人对象。
  • sql过程需要有3个select语句:客户机、地址和联系人
  • 在代码中,我们首先构造客户机
  • 然后,我们使用地址的clientid字段将address对象链接到正确的客户端
  • 我们对联系人也这样做

  • 您正在分析逗号分隔的字符串并分配属性吗?我很难理解这一点。多了解一些信息就好了。谢谢。如果您有三个不同的结果集,为什么需要类名?你不能按定义的顺序处理结果集吗?你可能还想使用某种ORM系统,例如Dapper:顺便问一下,为什么你的类是静态的(而属性不是)?@JleruOHeP没有特定的原因说明这些类是静态的。他们不一定是。我将研究这个简洁的例子。
    object obj;
    switch(className)
    {
        case "Foo":
            obj = new Foo();
            break;
        case "bar":
            obj = new Bar();
            break;
        ...
    }
    
    Dictionary<Guid, Client> clients = new Dictionary<Guid, Client>();
    
    // first read all the Client records.
    while (reader.Read())
    {
     Guid clientId = Guid.Parse(reader["Id"].ToString());
     Client tempClient = new Client();
     tempClient.Contacts = new List<Contact>();
     // construct the tempClient completely using all fields.
    
     clients.Add(clientId, tempClient);
    }
    
    // now move onto the address records.
    reader.nextResults();
    
    while (reader.Read())
    {
     Guid clientId = Guid.Parse(reader["ClientId"].ToString());
     clients[clientId].Address = new Address();
     // construct the clients[clientId].Address completely using all fields.
    }
    
    // now move onto the contact records.
    reader.nextResults();
    
    while (reader.Read())
    {
     Guid clientId = Guid.Parse(reader["ClientId"].ToString());
     Contact tempContact = new Contact();
     // construct the tempContact completely using all fields.
    
     clients[clientId].Contacts.Add(tempContact)
    }
    
    // at the end, the clients dictionary has all the Client objects linked with the right 
    // Address and Contact objects.