【发布时间】:2014-03-10 09:49:00
【问题描述】:
当这行 bckPk = Translate(packs); 执行时,我得到 Property set method not found. 错误,这是很自然的。但是有人可以建议我一个解决方法,通过它我可以实现我在这里尝试做的事情吗?
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Reflection;
namespace ExperimentProjects
{
public class ClassOne
{
public string PropertyOne { get; set; }
public string PropertyTwo { get; set; }
public string PropertyThree { get; set; }
}
public class ClassTwo
{
public string PropertyOne { get; set; }
public string PropertyTwo { get; set; }
public string PropertyThree { get; set; }
}
public class ClassPack : Collection<ClassOne>
{
}
public class ClassBckPack : Collection<ClassOne>
{
}
public class TranslateClass
{
public static TResult Translate<TSource, TResult>(TSource sourceObj)
{
Type typeOfSourceObj = sourceObj.GetType();
Type typeOfResultObj = typeof(TResult);
if (typeOfSourceObj.BaseType.Equals(typeOfResultObj.BaseType))
{
//Console.WriteLine("1");
}
ConstructorInfo constructorOfresultObj = typeOfResultObj.GetConstructor(System.Type.EmptyTypes);
Object[] parameters = new Object[0];
TResult result = (TResult)constructorOfresultObj.Invoke(parameters);
PropertyInfo[] propertiesInSourceObj = typeOfSourceObj.GetProperties();
if (propertiesInSourceObj.Length != 0)
{
foreach (PropertyInfo property in propertiesInSourceObj)
{
Console.WriteLine(property.PropertyType.Name);
PropertyInfo propertyOfResultObj = typeOfResultObj.GetProperty(property.Name);
if (propertyOfResultObj != null)
{
propertyOfResultObj.SetValue(result, property.GetValue(sourceObj));
}
}
}
Console.Read();
return result;
}
static void Main(string[] args)
{
ClassOne objOne = new ClassOne();
objOne.PropertyOne = "One";
objOne.PropertyTwo = "Two";
objOne.PropertyThree = "Three";
ClassTwo objTwo = Translate<ClassOne, ClassTwo>(objOne);
Console.WriteLine(objTwo.PropertyOne + " " + objTwo.PropertyTwo + " " + objTwo.PropertyThree);
ClassOne o = Translate<ClassOne, ClassOne>(objOne);
Console.WriteLine(o.PropertyOne + " " + o.PropertyTwo + " " + o.PropertyThree);
ClassPack packs = new ClassPack();
packs.Add(o);
packs.Add(objOne);
ClassBckPack bckPk = null;
try
{
bckPk = Translate<ClassPack, ClassBckPack>(packs);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.Read();
}
foreach (ClassOne eachObj in bckPk)
Console.WriteLine(eachObj.PropertyOne + " " + eachObj.PropertyTwo + " " + eachObj.PropertyThree);
Console.Read();
}
}
}
编辑:这里我想使用反射将对象从包复制到 bckPk,而不是使用 foreach 循环。例如下面这个例子:
Class Content
{
}
Class AContents : Collection<Content>
{
}
Class BContents : Collection<Content>
{
}
Class BusinessEntity
{
public AContents
{
get; set;
}
}
Class DataContract
{
public AContents
{
get; set;
}
}
I want to use this Translate method this way now :
BusinessEntity be= new BusinessEntity ();
DataContract dc= new DataContract ();
dc=Translate<BusinessEntity,DataContract>(be);
如果我运行此代码,则会抛出 找不到属性集方法错误
【问题讨论】:
-
那么你到底想做什么?在这种情况下,它试图设置
Count属性...您可以轻松判断属性是否可写(使用 CanWrite 属性),但我认为这对您没有太大帮助。 -
这只是我的印象,但是对泛型方法使用那么多反射感觉不对。在某些情况下,这当然是必要的。
-
请看编辑后的帖子
标签: c# reflection collections