Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 扩展(继承)实体框架类(不使用分部)_C#_Class_Inheritance_Entity Framework 4_Extend - Fatal编程技术网

C# 扩展(继承)实体框架类(不使用分部)

C# 扩展(继承)实体框架类(不使用分部),c#,class,inheritance,entity-framework-4,extend,C#,Class,Inheritance,Entity Framework 4,Extend,它是怎么开始的 我想在radGridView中添加两列,它们不在BusinessObjec集合中。特别是NewUrl和NewIdOnFilehost: 那我想做什么呢 我把这个放到网格里 radGridViewReuploadStatus.ItemsSource = FileHostings.Filesonic.getdAllDeletedLinks(); 然后我给他们添加了新的专栏 <telerik:GridViewColumn Header="New F.H.Id" UniqueNa

它是怎么开始的

我想在radGridView中添加两列,它们不在BusinessObjec集合中。特别是NewUrl和NewIdOnFilehost:

那我想做什么呢

我把这个放到网格里

radGridViewReuploadStatus.ItemsSource = FileHostings.Filesonic.getdAllDeletedLinks();
然后我给他们添加了新的专栏

<telerik:GridViewColumn Header="New F.H.Id" UniqueName="NewFilehostId" Width="*"></telerik:GridViewColumn>
                <telerik:GridViewColumn Header="New URL" UniqueName="NewUrl" Width="*"></telerik:GridViewColumn>
最后一个问题——我不明白什么是基本的

如何从DownloadLink扩展数据?我知道这是一个错误的名称约定,例如: 我如何从DownloadLink集合中列出扩展的数据?一定有比使用foreach更好的方法

现在我可能做错了

所以现在我应该做构造函数,并根据传入的DownloadLink设置dlExneded的每个属性

也许通过这样的思考是可行的

public DownloadLinkExtended(DownloadLink origDl){
        PropertyInfo[] myObjectProperties = origDl.GetType().GetProperties(); //BindingFlags.Public | BindingFlags.NonPublic
        foreach (PropertyInfo pi in myObjectProperties)
        {
            if (pi.GetValue(origDl, null) != null)
            {
                pi.SetValue(this, pi.GetValue(origDl, null), null);
            }
        }
    }
这太愚蠢了。那么,我对扩展类并向其添加新属性没有什么了解呢


我知道EF4类是部分的,我可以简单地通过部分类向它们添加属性,但我只希望它们用于网格,而不是其他任何地方。

若这些是EF类,而不是T4模板中的POCO,那个么您可能无论如何都不想从它们继承,因为您最终会遇到EF行李。除此之外,我认为还有一些潜在的解决方案

如果只在一个地方使用,则可以使用Linq中的投影改进for循环

var newthings = oldlinks.Select(old => new dlExtended{ NewUrl =old.NewUrl , NewIdOnFilehost =old.NewIdOnFilehost });
您还可以为dlExtended编写一个构造函数,该构造函数接受下载链接,然后执行以下操作

var newthings = oldlinks.Select(old => new dlExtended(old));
将属性复制放在一个位置


您还可以构建一个通用扩展方法,在两个对象之间复制具有相同名称的属性,并以多种方式使用它们。

我的浅层对象复制器与您的非常相似,但空测试有细微的不同。它还有一个方便的扩展方法包装器,所以它需要在一个静态类中

    /// <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 ShallowCopyTo<T>(this object sourceObject, ref T destObject)
    {
        Copy<T>(sourceObject,ref destObject);
    }
    /// <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 Copy<T>(object 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);
        }
    }
然而,我也有一个深度复制器,但这只适用于可序列化对象,所以请查看您从EDMX使用的代码生成,我认为它不会直接用于EF类,而是用于POCO生成的类

/// <summary>
/// Reference Article http://www.codeproject.com/KB/tips/SerializedObjectCloner.aspx
/// 
/// Provides a method for performing a deep copy of an object.
/// Binary Serialization is used to perform the copy.
/// </summary>

public static class ObjectCopier
{
    /// <summary>
    /// Perform a deep Copy of the object.
    /// </summary>
    /// <typeparam name="T">The type of object being copied.</typeparam>
    /// <param name="source">The object instance to copy.</param>
    /// <returns>The copied object.</returns>
    public static T Clone<T>(this T source)
    {
        if (!typeof(T).IsSerializable)
        {
            throw new ArgumentException("The type must be serializable.", "source");
        }

        // Don't serialize a null object, simply return the default for that object
        if (Object.ReferenceEquals(source, null))
        {
            return default(T);
        }

        IFormatter formatter = new BinaryFormatter();
        Stream stream = new MemoryStream();
        using (stream)
        {
            formatter.Serialize(stream, source);
            stream.Seek(0, SeekOrigin.Begin);
            return (T)formatter.Deserialize(stream);
        }
    }

一个可能的解决方案的表述:如何继承类,以便具有父类的参数实例的构造函数可以设置所有继承的属性?谢谢您的回答。你的第一个方法会很好,但我想在不止一个地方使用这个新类。在第二种方法中,我仍然需要像这样指定每个属性;this.property2=orig.property2;此.property3=原始属性3;塔特说得对吗?这正是我不想做的。在两个对象之间复制同名属性的扩展方法实际上是我遇到的问题,因为我尝试使用的方法不处理其他EF类的属性。无论如何,在快速google之后,我认为您提到的POCO,更具体地说,在MVVM中使用它作为类似ModelView的东西可能是我正在寻求的解决方案,但是——我在这里第一次听说了poco,因此需要花时间来研究:,我仍然想知道如何正确地进行这种反射。我认为将EF类传递到视图中是很少见的。它可以工作,所有的例子都可以,但实际上几乎所有的真实场景都需要一个POCO Viewmodel——一个表示视图所需数据的类。当数据库发生变化时保持这些同步是一个问题,而实例化大量数据的性能则是另一个问题。如果是复制,我会用我的对象复制代码发布另一个答案,以防万一。事实上,我很惊讶你真的了解什么是问题以及我试图解决什么问题。这不是实际的应用程序,它更多的是用于学习目的。你知道有任何关于保持viewmodels与dbb同步的最佳实践/自动化的指南吗?我可能不知道你在做什么:-但我知道我做了什么,以及我所学到的建议。我还使用了Telerik控件,主要是MVC控件,并且知道如果涉及任何JSON序列化,它会对原始EF类产生兴趣。在MVC中,将POST绑定到EF模型至少是危险的,因为任何与属性匹配的POST数据在默认情况下都是绑定的。无论如何,同步viewmodels-如果您发现比我发布的内容更好的内容,请添加此内容:-
/// <summary>
/// Reference Article http://www.codeproject.com/KB/tips/SerializedObjectCloner.aspx
/// 
/// Provides a method for performing a deep copy of an object.
/// Binary Serialization is used to perform the copy.
/// </summary>

public static class ObjectCopier
{
    /// <summary>
    /// Perform a deep Copy of the object.
    /// </summary>
    /// <typeparam name="T">The type of object being copied.</typeparam>
    /// <param name="source">The object instance to copy.</param>
    /// <returns>The copied object.</returns>
    public static T Clone<T>(this T source)
    {
        if (!typeof(T).IsSerializable)
        {
            throw new ArgumentException("The type must be serializable.", "source");
        }

        // Don't serialize a null object, simply return the default for that object
        if (Object.ReferenceEquals(source, null))
        {
            return default(T);
        }

        IFormatter formatter = new BinaryFormatter();
        Stream stream = new MemoryStream();
        using (stream)
        {
            formatter.Serialize(stream, source);
            stream.Seek(0, SeekOrigin.Begin);
            return (T)formatter.Deserialize(stream);
        }
    }