當這一行bckPk = Translate(packs);執行我得到未找到屬性集方法。這是很自然的錯誤。但有人可以建議我一個解決方法,我可以通過這個解決方案實現我在這裏要做的事情嗎?找不到屬性設置方法
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();
}
}
}
編輯:在這裏我想複製對象從包使用反射,而不是使用foreach循環的bckPk。例如藉此下面的例子:
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屬性),但我認爲這不會對您有太大的幫助。 –
這只是我的印象,但對於泛型方法使用這麼多反射感覺不對。在某些情況下,當然可能有必要。 – Dirk
請參閱編輯後文章 –