将类型转换为 class
Cast a Type to a class
如果我有这样定义的class:
public class className
{
public object this[string propertyName]
{
get { return this.GetType().GetProperty(propertyName).GetValue(this, null); }
set { this.GetType().GetProperty(propertyName).SetValue(this, value, null); }
}
public string Foo{ get; set; }
public string Bar { get; set; }
我当然可以像这样设置和获取值:
className d = new className();
d["Foo"]="abcd" // set
string s = (string)f["Bar"];
(感谢Eduardo Cuomo for his answer here)
但我真正想做的是:
Type target = Type.GetType(DocumentType);
// loop through list of key-value pairs and populate the data class defined in target object Type
foreach (Dictionary<string, string> PQList in LPQReq)
{
foreach (KeyValuePair<string, string> kvp in PQList)
{
// populate the member in the data class with the value from the MQ String
target[kvp.Key] = kvp.Value;
}
但这不会编译为 Cannot apply indexing with [] to an expression of type 'System.Type'
那我该怎么做呢?
我当然可以使用 dynamic
,但也许有一种方法可以将我的类型转换为我的目标 Class?
你可以通过反思来做到这一点。假设所有可能的 DocumentType
都有一个无参数的构造函数,你可以这样做:
// Get the type (this comes from your example)
Type target = Type.GetType(DocumentType);
// Create an instance (that's the important part that was missing)
object instance = Activator.CreateInstance(target);
foreach (Dictionary<string, string> PQList in LPQReq) {
foreach (KeyValuePair<string, string> kvp in PQList) {
// This code again comes from your example,
// except propertyName is kvp.Key and value is kvp.Value
target.GetProperty(kvp.Key).SetValue(instance, kvp.Value, null);
}
}
您需要实例化类型才能访问索引器,并且需要将其转换为具有索引器的对象。
您可以定义一个接口:
public interface IIndexable_String
{
object this[string index]
{
get;
set;
}
}
将其应用于您的 classes:
public class someclass : IIndexable_String
然后实例化实例并访问索引器。
Type target = Type.GetType(DocumentType);
// Instantiate
IIndexable_String instance = (IIndexable_String)Activator.CreateInstance(target);
foreach (Dictionary<string, string> PQList in LPQReq)
{
foreach (KeyValuePair<string, string> kvp in PQList)
{
// populate the member in the data class with the value from the MQ String
instance[kvp.Key] = kvp.Value;
}
当然,如果你像@dasblinkenlight 那样做,你甚至不需要 class 中的神奇 getter 和 setter,也不需要界面。
如果我有这样定义的class:
public class className
{
public object this[string propertyName]
{
get { return this.GetType().GetProperty(propertyName).GetValue(this, null); }
set { this.GetType().GetProperty(propertyName).SetValue(this, value, null); }
}
public string Foo{ get; set; }
public string Bar { get; set; }
我当然可以像这样设置和获取值:
className d = new className();
d["Foo"]="abcd" // set
string s = (string)f["Bar"];
(感谢Eduardo Cuomo for his answer here)
但我真正想做的是:
Type target = Type.GetType(DocumentType);
// loop through list of key-value pairs and populate the data class defined in target object Type
foreach (Dictionary<string, string> PQList in LPQReq)
{
foreach (KeyValuePair<string, string> kvp in PQList)
{
// populate the member in the data class with the value from the MQ String
target[kvp.Key] = kvp.Value;
}
但这不会编译为 Cannot apply indexing with [] to an expression of type 'System.Type'
那我该怎么做呢?
我当然可以使用 dynamic
,但也许有一种方法可以将我的类型转换为我的目标 Class?
你可以通过反思来做到这一点。假设所有可能的 DocumentType
都有一个无参数的构造函数,你可以这样做:
// Get the type (this comes from your example)
Type target = Type.GetType(DocumentType);
// Create an instance (that's the important part that was missing)
object instance = Activator.CreateInstance(target);
foreach (Dictionary<string, string> PQList in LPQReq) {
foreach (KeyValuePair<string, string> kvp in PQList) {
// This code again comes from your example,
// except propertyName is kvp.Key and value is kvp.Value
target.GetProperty(kvp.Key).SetValue(instance, kvp.Value, null);
}
}
您需要实例化类型才能访问索引器,并且需要将其转换为具有索引器的对象。
您可以定义一个接口:
public interface IIndexable_String
{
object this[string index]
{
get;
set;
}
}
将其应用于您的 classes:
public class someclass : IIndexable_String
然后实例化实例并访问索引器。
Type target = Type.GetType(DocumentType);
// Instantiate
IIndexable_String instance = (IIndexable_String)Activator.CreateInstance(target);
foreach (Dictionary<string, string> PQList in LPQReq)
{
foreach (KeyValuePair<string, string> kvp in PQList)
{
// populate the member in the data class with the value from the MQ String
instance[kvp.Key] = kvp.Value;
}
当然,如果你像@dasblinkenlight 那样做,你甚至不需要 class 中的神奇 getter 和 setter,也不需要界面。