C# 获取基本的、复杂的、ArrayEnumerable类型

C# 获取基本的、复杂的、ArrayEnumerable类型,c#,reflection,C#,Reflection,我的每个数据库实体都有一个单独的类,当我创建一个类的对象来引用一个类的属性时,它会返回一个循环引用,其中包含通过FK关联的其他实体的属性。。。要删除循环引用,我想首先通过“上下文代理对象”复制对象的副本,然后获取该对象的基本、复杂、可数组计算的类型,并从对象中删除这些类型,然后通过web服务返回该对象……听起来像一个递归的浅层克隆。我使用了下面的方法,但只使用了一个级别 public static class EntityBaseExtensions { /// <summary&

我的每个数据库实体都有一个单独的类,当我创建一个类的对象来引用一个类的属性时,它会返回一个循环引用,其中包含通过FK关联的其他实体的属性。。。要删除循环引用,我想首先通过“上下文代理对象”复制对象的副本,然后获取该对象的基本、复杂、可数组计算的类型,并从对象中删除这些类型,然后通过web服务返回该对象……

听起来像一个递归的浅层克隆。我使用了下面的方法,但只使用了一个级别

public static class EntityBaseExtensions
{
    /// <summary>
    /// Description:    Creates a non-recursive shallow copy of an entity, only including public instance properties decorated with ColumnAttribute.
    ///                 This will return an object without entity references.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="source"></param>
    /// <returns>A non-recursive shallow copy of a LINQ entity</returns>
    public static T ShallowClone<T>(this T source) where T : EntityBaseClass
    {
        // create an object to copy values into
        T destination = Activator.CreateInstance<T>();

        // get source and destination property infos for all public instance
        PropertyInfo[] sourcePropInfos = source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
        PropertyInfo[] destinationPropInfos = source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

        foreach (PropertyInfo sourcePropInfo in sourcePropInfos)
        {
            if (Attribute.GetCustomAttribute(sourcePropInfo, typeof(ColumnAttribute), false) != null)
            {
                PropertyInfo destPropInfo = destinationPropInfos.Where(pi => pi.Name == sourcePropInfo.Name).First();

                destPropInfo.SetValue(destination, sourcePropInfo.GetValue(source, null), null);
            }
        }

        return destination;
    }

}
公共静态类EntityBaseExtensions
{
/// 
///描述:创建实体的非递归浅层副本,仅包括用ColumnAttribute修饰的公共实例属性。
///这将返回一个没有实体引用的对象。
/// 
/// 
/// 
///LINQ实体的非递归浅拷贝
公共静态T ShallowClone(此T源),其中T:EntityBaseClass
{
//创建要将值复制到其中的对象
T destination=Activator.CreateInstance();
//获取所有公共实例的源和目标属性信息
PropertyInfo[]SourcePropInfo=source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
PropertyInfo[]DestinationPropInfo=source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach(sourcePropInfo中的PropertyInfo sourcePropInfo)
{
if(Attribute.GetCustomAttribute(sourcePropInfo,typeof(ColumnAttribute),false)!=null)
{
PropertyInfo destPropInfo=destinationpropinfo.Where(pi=>pi.Name==sourcePropInfo.Name).First();
destPropInfo.SetValue(目标,sourcePropInfo.GetValue(源,null),null);
}
}
返回目的地;
}
}

我建议您为您想要实现的目标编写一些代码示例。你对这个问题的描述不够清楚(至少对我来说)。