简体   繁体   English

通用铸造问题C#

[英]Generic casting question C#

I have a function which takes a generic: 我有一个通用的函数:

public static ShowTrackChangesViewModel CreateVM<T>(IList<TrackChanges> TrackChanges, T entity)
        {
            //How do i access T properties ?

        }

T here is an entityFramework object. 这里是一个entityFramework对象。 How do i cast it back to the real one to access its property ? 我如何将它转回真实的访问其财产? Do i need to write a big if code ? 我需要写一个大的if代码吗? Thx 谢谢

You can add this little gem to say that T should be an object of type MyBaseType : 你可以添加这个小宝石来说T应该是MyBaseType类型的对象:

public static ShowTrackChangesViewModel CreateVM<T>(IList<TrackChanges> TrackChanges, T entity) where T : MyBaseType

Then, you can use entity as if it were a MyBaseType. 然后,您可以使用entity ,就像它是MyBaseType一样。 If you need specific properties that aren't in a base class, then you don't want a generic method (because it wouldn't be generic!). 如果您需要不在基类中的特定属性,那么您不需要通用方法(因为它不是通用的!)。

Hope that helps! 希望有所帮助!

To be honest, I don't think generics are appropriate for your situation, at least not from what I can determine from your question. 说实话,我不认为仿制药适合你的情况,至少不能从我的问题中确定。

The suggestion to constrain T: 约束T的建议:

public static ShowTrackChangesViewModel CreateVM<T>(IList<TrackChanges> TrackChanges, T entity) where T : MyBaseType

is equivalent to the simpler: 相当于更简单:

public static ShowTrackChangesViewModel CreateVM(IList<TrackChanges> TrackChanges, MyBaseType entity)

You get no benefit from using generics here. 在这里使用泛型没有任何好处。

Assuming you want this method to perform similar operations on instances of several different classes, another option as others have already mentioned, is to get the method to accept a common base class that supports the operations you need. 假设您希望此方法对多个不同类的实例执行类似的操作,其他选项(如其他人已经提到的那样)是让方法接受支持所需操作的公共基类。

If you're lucky, this base class already exists, or you can alter the type hierarchy and factor one in. If the classes are auto-generated, you can usually get them to implement an interface without altering their code directly (you certainly could with LINQ to SQL, not sure about the entity framework). 如果你很幸运,这个基类已经存在,或者你可以改变类型层次结构和因子。如果类是自动生成的,你通常可以让它们实现一个接口而不直接改变它们的代码(你当然可以使用LINQ to SQL,不确定实体框架)。

To do this, you create a partial class which implements the interface. 为此,您需要创建一个实现该接口的分部类。 Say you have a class Person which has code you can't touch, create your interface: 假设您有一个类别,其中包含您无法触及的代码,请创建您的界面:

public interface ISupportCreateVm
{
    void SpecialOperationRequiredForCreateVM();
}

create a partial class in a new file like so: 在新文件中创建一个部分类,如下所示:

public partial class Person : ISupportCreateVm
{
    void SpecialOperationRequiredForCreateVM()
    {
        // TODO: Implement me!
    }
}

You could then write your method as: 然后,您可以将您的方法编写为:

public static ShowTrackChangesViewModel CreateVM(IList<TrackChanges> TrackChanges, ISupportCreateVm entity)

and access the SpecialOperationRequiredForCreateVM() method on whatever you pass in. 并在您传入的任何内容上访问SpecialOperationRequiredForCreateVM()方法。

The trouble is you'll have to do this for every class you want to pass to CreateVM, which is something I'm guessing you were looking at generics for in order to avoid in the first place! 麻烦的是你必须为你要传递给CreateVM的每个类都做这个,这是我猜你正在寻找泛型的东西,以便首先避免! However, it certainly beats a massive block of if statements or a huge switch in terms of OO design. 然而,它肯定胜过大量的i​​f语句或OO设计方面的巨大转变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM