【发布时间】:2014-09-01 20:09:31
【问题描述】:
我有一个基类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassPropertyTest
{
public class BaseClass
{
public string name { get; set; }
public double value { get; set; }
}
}
然后我有一些从基类派生的类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassPropertyTest
{
public class ClassA : BaseClass
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassPropertyTest
{
public class ClassB : BaseClass
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassPropertyTest
{
public class ClassC : BaseClass
{
}
}
我还有一个主类,它具有每个类实例的属性。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassPropertyTest
{
public class MasterClass
{
public ClassA ClassA { get; set; }
public ClassB ClassB { get; set; }
public ClassC ClassC { get; set; }
}
}
现在,当我收到主类的实例时,我需要从属性 ClassA 创建一个新类。我使用反射,但我不断收到目标无效的错误。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ClassPropertyTest
{
class Program
{
static void Main(string[] args)
{
//Fill master class
MasterClass o_masterclass = new MasterClass();
o_masterclass.ClassA = new ClassA { name = "classA", value = 1 };
o_masterclass.ClassB = new ClassB { name = "classB", value = 2 };
o_masterclass.ClassC = new ClassC { name = "classc", value = 3 };
//Now get class by property name
foreach (PropertyInfo prop in o_masterclass.GetType().GetProperties())
{
//Check for class A property
if (prop.Name == "ClassA") {
//Create an instance of that type from ClassA
BaseClass instance = (BaseClass)Activator.CreateInstance(prop.PropertyType.BaseType);
//Copy properties from masterclass property ClassA to new instance of BaseClass
CopyObject<BaseClass>(prop.PropertyType.BaseType, ref instance);
}
}
Console.ReadKey();
}
/// <summary>
/// Copy an object to destination object, only matching fields will be copied
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sourceObject">An object with matching fields of the destination object</param>
/// <param name="destObject">Destination object, must already be created</param>
public static void CopyObject<T>(Type sourceObject, ref T destObject)
{
// If either the source, or destination is null, return
if (sourceObject == null || destObject == null)
return;
// Get the type of each object
Type sourceType = sourceObject;//.GetType();
Type targetType = destObject.GetType();
// Loop through the source properties
foreach (PropertyInfo p in sourceType.GetProperties())
{
// Get the matching property in the destination object
PropertyInfo targetObj = targetType.GetProperty(p.Name);
// If there is none, skip
if (targetObj == null)
continue;
// Set the value in the destination
targetObj.SetValue(destObject, p.GetValue(sourceObject, null), null);
}
}
}
}
有人可以帮我吗?谢谢。
@Sergey,我已将 CopyObject 方法更改为:
public static void CopyObject<T>(T sourceObject, ref T destObject)
{
// If either the source, or destination is null, return
if (sourceObject == null || destObject == null)
return;
// Get the type of each object
Type sourceType = sourceObject.GetType();
Type targetType = destObject.GetType();
// Loop through the source properties
foreach (PropertyInfo p in sourceType.GetProperties())
{
// Get the matching property in the destination object
PropertyInfo targetObj = targetType.GetProperty(p.Name);
// If there is none, skip
if (targetObj == null)
continue;
// Set the value in the destination
targetObj.SetValue(destObject, p.GetValue(sourceObject, null), null);
}
}
当我像你告诉我的那样称呼它时,它会起作用。
CopyObject<BaseClass>(o_masterclass.ClassA, ref instance);
但在我的情况下,o_masterclass.ClassA 实际上是 prop.PropertyType.BaseType。如何将此类型转换为所需的对象?
【问题讨论】:
-
在 CopyObject 方法中:
p.GetValue(sourceObject, null):在这种情况下,sourceObject是一种类型,而不是该类型的实例...
标签: c# reflection properties