传递具有相同属性的不同对象 [英] C# Generic Passing different objects with same properties

查看:198
本文介绍了传递具有相同属性的不同对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我在做什么是可能的。
我有2种方法。 2方法的正文完全相同,但是参数和返回方法的签名是不同的。传入的参数属性已更改,对象不同,但具有相同的属性名称(它们是两个不同的实体框架实体)。继承基础对象是不可能的(我认为),因为这些是实体框架实体。



最好展示示例,然后谈论它..



方法1

  private static IQueryable< MapListing> ApplyMapFilterToListings(IQueryable< MapListing>列表,ListSearchCriteria条件,bool boolIsPremiumListingsOnly = false)
{
//设置列表属性(MapListing Entity类型)
}

方法2

  private static IQueryable< ; vListing> ApplyFilterToListings(IQueryable< vListing> listing,ListingSearchCriteria criteria,bool boolIsPremiumListingsOnly = false)
{
//设置列表的属性(类型为vListing Entity)
}

通常我不介意如果函数的正文完全相同,但是在这种情况下,很多属性都是有条件地设置的,我想要使这些方法完全相同,并且笨蛋证明他们不同步。



还有原因我正在重试IQueryable<>是因为我在返回

解决方案

我有两个选择。


  1. 实现界面



    如果创建界面包含所有共享属性和方法,您可以创建一个接受该接口的方法,或者在使用泛型时,实现它的所有类型。 (是的,可以在EF生成的类中实现接口


  2. 使用动态



    动态关键字将给你很多的自由。你可以假装一个属性或方法是否存在。当您传入对象时,会在运行时检查。您应该谨慎使用 dynamic ,因为它可能会损害您的软件质量。


首选项是首选项。这是最好的解决方案,但它需要您访问课程。如果课程在第三方库中,动态可能是您唯一的解决方案。


I am not sure if what I am doing is possible. I have 2 methods. The body of the 2 methods are exactly identical, however the signature of the method both parameter and return are different. The passed in parameter's properties are changed and the object are different but have the same property name (They are two different Entity Framwork Entities). Inheriting both from a base object is not possible (i think) because these are Entity Framework Entities.

Best to show example then talk about it..

Method 1

private static IQueryable<MapListing> ApplyMapFilterToListings(IQueryable<MapListing> listings, ListingSearchCriteria criteria, bool boolIsPremiumListingsOnly = false)
{
    //setting properties of listings (of type MapListing Entity)
}

Method 2

private static IQueryable<vListing> ApplyFilterToListings(IQueryable<vListing> listings, ListingSearchCriteria criteria, bool boolIsPremiumListingsOnly = false)
{
    //setting properties of listings (of type vListing Entity)
}

Generally I would not mind if the body of the function is exactly identical, however in this case a lot of properties are set conditionally and I want to make these methods exactly identical and idiot proof that they go out of sync.

Also reason I am returing IQueryable<> is because I do other stuff to it after returning

解决方案

You have two options in my opinion.

  1. Implement an interface

    If you create an interface containing all shared properties and methods, you can create one method accepting that interface, or when using generics, all types implementing it. (Yes, it is possible to implement an interface in EF generated classes)

  2. Use dynamic

    The dynamic keyword will give you a lot of freedom. You can pretend if a property or method exists. This is checked at runtime when you pass in the object. You should use dynamic with care, since it can harm your software quality.

The first option is preferred. It is the best solution, but it requires you to have access over the classes. If the classes are in a third party library, dynamic is probably your only solution.

这篇关于传递具有相同属性的不同对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆