问题描述
我在 c# 中使用多个工作区,这些工作区具有一个特定类,每个工作区中的类始终相同.我希望能够拥有此类的副本,以便能够在不处理名称空间差异的情况下使用它.示例:
I'm working in c# with several workspaces that have one specific class which his always the same in each workspace.I would like to be able have a copy of this class to be able to work with it without dealing with namespaces differences.example :
namespace1 {
class class1{
public class2;
}
class class2{
public string;
}
}
namespace2 {
class class1{
public class2;
}
class class2{
public string;
}
}
在我复制的类中,我有一个函数可以将所有数据复制到命名空间的类之一.如果我只有 c# 标准类型,它就可以工作.一旦我处理 class2 对象(也来自不同的命名空间),我就得到了例外(对象与目标类型不匹配.")
In my copied Class I've got a function to copy all data's to one of the namespace's class.It's working if i only have c# standard types. I got exeption ( "Object does not match target type." ) as soon as I'm dealing with class2 object (which is also from different namespaces)
public Object toNamespaceClass(Object namespaceClass)
{
try
{
Type fromType = this.GetType();
Type toType = namespaceClass.GetType();
PropertyInfo[] fromProps = fromType.GetProperties();
PropertyInfo[] toProps = toType.GetProperties();
for (int i = 0; i < fromProps.Length; i++)
{
PropertyInfo fromProp = fromProps[i];
PropertyInfo toProp = toType.GetProperty(fromProp.Name);
if (toProp != null)
{
toProp.SetValue(this, fromProp.GetValue(namespaceClass, null), null);
}
}
}
catch (Exception ex)
{
}
return namespaceClass;
}
任何人都知道如何处理这种递归反射".
Anyone do have any idea of how to deal with this kind of "recursivity reflection".
我希望一切都是可以理解的.
I hope eveything is understandable.
谢谢,再见!
我想我已经解决了(至少在我看来),明天我会在工作中尝试解决方案.如果属性不是标准类型,将我的函数从我的类中取出并递归使用它可能是解决方案.
Edit :I think i got it solved (at least in my mind), I'll try the solution back at work tomorrow. Taking my function out of my class and using it recursively if a property is not a standard type is maybe the solution.
推荐答案
我已经解决了,只是为了让你知道我是如何做到的:这个解决方案非常完美,因为它只处理一维数组而不是更多.
I got it solved , just to let you know how I did it :This solution is sot perfect because it handle only 1 dimensions array not more.
public static Object CopyObject(Object from , Object to)
{
try
{
Type fromType = from.GetType();
Type toType = to.GetType();
PropertyInfo[] fromProps = fromType.GetProperties();
PropertyInfo[] toProps = toType.GetProperties();
for (int i = 0; i < fromProps.Length; i++)
{
PropertyInfo fromProp = fromProps[i];
PropertyInfo toProp = toType.GetProperty(fromProp.Name);
if (toProp != null)
{
if (toProp.PropertyType.Module.ScopeName != "CommonLanguageRuntimeLibrary")
{
if (!toProp.PropertyType.IsArray)
{
ConstructorInfo ci = toProp.PropertyType.GetConstructor(new Type[0]);
if (ci != null)
{
toProp.SetValue(to, ci.Invoke(null), null);
toProp.SetValue(to, gestionRefelexion.CopyObject(fromProp.GetValue(from, null), toProp.GetValue(to, null)), null);
}
}
else
{
Type typeToArray = toProp.PropertyType.GetElementType();
Array fromArray = fromProp.GetValue(from, null) as Array;
toProp.SetValue(to, copyArray(fromArray, typeToArray), null);
}
}
else
{
toProp.SetValue(to, fromProp.GetValue(from, null), null);
}
}
}
}
catch (Exception ex)
{
}
return to;
}
public static Array copyArray(Array from, Type toType)
{
Array toArray =null;
if (from != null)
{
toArray= Array.CreateInstance(toType, from.Length);
for (int i = 0; i < from.Length; i++)
{
ConstructorInfo ci = toType.GetConstructor(new Type[0]);
if (ci != null)
{
toArray.SetValue(ci.Invoke(null), i);
toArray.SetValue(gestionRefelexion.CopyObject(from.GetValue(i), toArray.GetValue(i)), i);
}
}
}
return toArray;
}
希望这可以帮助一些人.谢谢大家的帮助.干杯
Hope this can help some people.Thanks for helping everyone.Cheers
这篇关于复制两个具有不同命名空间的相同对象(递归反射)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!