English 中文(简体)
通用或公正的超负荷使用?
原标题:Generics or just use overloads?

我有3个班级,都拥有类似的财产。 在所有3个类别中,3个财产的名称完全相同。 相反,这3种方法(每类一种方法)是我在此可以采用某种方式?

public static String GetAString(ActivityMedia activityMedia)
{
   return activityMedia.name;
}

public static String GetAString(AuditMedia auditMedia)
{
   return auditMedia.name;
}

public static String GetAString(VehicleMedia vehicleMedia)
{
   return vehicleMedia.name;
}

EDIT:显然,目的是简单地将这三类中的一种物体转至GetAString(CDR)

最佳回答
  1. introduce interface with a single string Name property
  2. mark all three classes by interface
  3. Now you can assess media Name directly I ve no idea why you need a method for this but anyway, you can create extension method for the interface type IMedia
interface IMedia
{
    string Name { get; }
}

class ActivityMedia : IMedia
class AuditMedia : IMedia
class VehicleMedia : IMedia

static class MediaExtensions
{
   public static string GetName(this IMedia instance)
   {
          return instance.Name;
   }
}
问题回答

另一种选择是使用接口......

public interface INameable
{
   String Name { get; }
}

public static String GetAString(INameable nameable)
{
   return nameable.Name;
}

虽然此时此刻,你甚至可能不需要“老幼”方法?

您可以通过接口这样做。

public interface INamedMedia
{
    string Name { get; }
}

then your method becomes

public static String GetAString(INamedMedia media)
{
    return media.Name;
}

缩略语

public class Media : INamedMedia
{
    public string Name { get { return "Media"; } }
}

我本会认为,一个接口将是这里的理想解决办法:

public interface IMedia
{
    public String Name { get; }
}

and then you could have your static method taking that interface type as it s parameter:

    public static String GetAString(IMedia media)
    {
        return media.Name;
    }

或者,你的所有媒体课程都可能来自包含你要求的财产的基类,并且将这种财产输入方法而不是接口。

很难告诉你,这是更好的选择,因为它取决于你申请的复杂性和继承链。

http://www.c-sharpcorner.com/UploadFile/dipenlama22/Inheritance_Versus_Interfaces071420060431AM/Inheritance_Versus_Interfaces.aspx”rel=“nofollow”article 或许可以提供帮助

如果它们完全没有关系,则一种选择是使用<代码>dnamic。 (如果您重读C# 4):

public static String GetAString(dynamic d){
    return d.name;
}

Edit:

如果你不使用C#4.0,你可以进行反思:

public static string GetAString(object o)
{
    System.Reflection.PropertyInfo name = o.GetType().GetProperty("name");
    return (string) name.GetValue(o, null);
} 

你们不需要非专利,你们需要OOP和继承。

ActualMedia,AuditMediaVehicleMedia 要么实施基调,如<代码>IMedia,要么产生一个基本类别,如<编码>MediaBase。

然后,您只读到<代码>GetAString,接受<代码>IMedia 反对或MediaBase

另一种选择是,在<条形码>上,在<条码>主动性Media、<条码>、和<条码>上,你只将姓名财产退回到<条码>上,无需任何<条码>。

如果这些阶层没有共同的祖先,他们会在这里获得帮助。 解决办法是,通过申报与<姓名/代码>财产接口,安排他们分享共同祖先财产。 每个班级都实施这一接口,然后可以有一个单独的<代码>GetAString功能。

如果这三类人拥有一套相同的特性,那么你就能够更好地执行一个基类,而采用三种媒体课程在基类宣布的共同财产和方法。

public class ActivityMedia : Media 
{

}

public class Media
{
     public string Name {get;set;}
}

这样,这三类方法就在一个地方界定了一套方法,使法典更加易于维护。 在给予你灵活的情况下,可以将特定类别方法用于其衍生的执行。

在这里执行继承比一般遗产更符合逻辑。

您也可使用<条码>Interface,但这将使<条码>Name所有您的<条码>-Media、<条码>AuditMedia、<条码>VehicleMedia各栏上的财产重复。

考虑一个基类<代码>Media。

public class Media
{
    // consider all properties that are common
    // on Media domain
    public string Name { get; set }
}

并继承ActectiveMedia,AuditMedia,VehicleMedia from Media

public class ActivityMedia : Media
{
    // other properties on ActivityMedia domain
}

public class AuditMedia : Media
{
    // other properties on AuditMedia domain
}

public class VehicleMedia : Media
{
    // other properties on VehicleMedia domain
}

我们现在使用<条码>Media等值,载于<条码>。

public static String GetAString(Media activityMedia)
{
   return activityMedia.name;
}

public static String GetAString(Media auditMedia)
{
   return auditMedia.name;
}

public static String GetAString(Media vehicleMedia)
{
   return vehicleMedia.name;
}

This is where inheritance comes to your rescue. Have such base class:

public class Media
{
    public string name { get { return "Media"; } }
}

之后,每一阶层将继承和界定自己的名称:

public class ActivityMedia : Media
{
    public new string name { get { return "Activity Media"; } }
}

最后:

public static String GetAString(Media media)
{
    return media.name;
}

Edit:当你重新坚持你目前的设计时,你可以进行反思:

public static String GetAString(object media)
{
    PropertyInfo propName = media.GetType().GetProperty("name");
    if (propName != null)
        return propName.GetValue(media, null);
    return "";
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签