3

我有几个对象,它们具有一些共同的属性。例如:

对象 A 属于 X 类型,它有 10 个属性

对象 B 的类型为 Y,它有 15 个属性

对象 C 是 Z 类型,它有 7 个属性

所有这些对象都具有共同的“名称”、“签名”和“校验和”属性。我正在尝试创建一个静态辅助方法,它可以接受包含“名称”、“签名”和“校验和”属性的任何类型的对象。这是可能的还是我实际上需要三种不同的方法(一个接受每种类型的对象)?

编辑 - 对于它的价值,我没有提到这些对象是通过 Web 服务向我公开的。

4

8 回答 8

8

您应该将这些属性移动到公共基类或接口并使用它。

于 2011-12-14T19:19:10.603 回答
4

你有两个不错的选择。首先是继承:

public class CommonObject
{
    public string Name;
    public string Signature;
    public string Checksum;
}

public class X : CommonObject
{
    // other properties
}

public class Y : CommonObject
{
    // other properties
}

public class Z : CommonObject
{
    // other properties
}

public static void DoSomething(CommonObject o)
{
    // You can access these values
    if (o.Name == "" || o.Signature == "")
        o.Checksum = 0;
}

这可能很强大,因为您可以使这些属性虚拟化,并且每个类都可以覆盖以不同方式处理它们。

第二个选项是使用接口:

public class OtherClass
{
    public static void DoSomething(CommonObject o)
    {
        // code here
    }
}

public interface CommonObject
{
    string Name { get; }
    string Signature { get; }
    string Checksum { get; }
}

public class X : CommonObject
{
    private string _name = "";
    private string _signature = "";
    private string _checksum = "";

    string CommonObject.Name { get { return _name; } }
    string CommonObject.Signature { get { return _signature; } }
    string CommonObject.Checksum { get { return _checksum; } }
}
于 2011-12-14T19:28:52.713 回答
2

有可能...如果您定义一个包含公共方法的公共基类,并使对象 A、B 和 C 成为基类的子类。然后,您的静态辅助方法可以使用基类作为其参数类型,并且可以将任何子类型传递给它。

于 2011-12-14T19:19:21.947 回答
2

我相信界面是你最好的选择,

public interface ISomeGoodNameForCommonProperies 
{
  string Name {get;set;}
  string Signature {get;set;}
  string Checksum {get;set;}
}

public class X : ISomeGoodNameForCommonProperies 
{
  string Name {get;set;}
  string Signature {get;set;}
  string Checksum {get;set;}
  ...
}

public class Y : ISomeGoodNameForCommonProperies 
{
  string Name {get;set;}
  string Signature {get;set;}
  string Checksum {get;set;}
  ...
}

public class Z : ISomeGoodNameForCommonProperies 
{
  string Name {get;set;}
  string Signature {get;set;}
  string Checksum {get;set;}
  ...
}

然后,您的辅助方法将采用 ISomeGoodNameForCommonProperies

public object MyHelperMethod(ISomeGoodNameForCommonProperies myObject)
{
  ...
}

继承当然会起作用,但是我会避免使用基类,除非它对您尝试创建的对象有意义。您应该问自己的问题是,X、Y 和 Z 是否可以定义为某种类型的不同对象 O?如果是这样,请继续创建 O 并从中固有。如果您共有的这 3 个属性不足以定义逻辑实体,但出于实际原因需要将它们组合在一起,那么接口是您的最佳选择。

于 2011-12-14T19:25:48.120 回答
2

对于另一种选择,还有动态类型。尽管此选项应该适合您,但我肯定会尝试使用其他人提到的接口或基类。

于 2011-12-14T19:28:42.987 回答
2

我将假设由于这些对象是通过 Web 服务公开的,因此您无法控制对象的定义。它们就是它们,你不能让它们从公共基类或接口继承。有了这个问题的限制,你真的只有两个选择。

如果您使用的是 C# 4.0 或更高版本,则可以使用新的动态类型。这基本上是一个对象引用,直到运行时才进行任何类型评估。因此,如果您在动态类型上使用的属性或方法不存在,您将获得运行时错误,而不是编译期间的错误。

另一种选择是简单地引用 Object 类型并使用反射来操作属性和方法。那里有很多潜在的丑陋。

如果您没有使用 C# 4+,那么我想我会使用这三种单独的方法。虽然您可能会复制一些代码,但我宁愿拥有它,而不是一堆复杂的难以遵循的反射调用,您必须在 c# 3.5 中使用-

于 2011-12-14T22:37:00.060 回答
1

您可以使用继承

于 2011-12-14T19:19:59.573 回答
1

这个问题有两种方法:

创建一个具有所有这些通用属性的基类,并从中派生其他属性。

public abstract class MyBaseClass
{
    public string Name { get; set; }
    public string Signature { get; set; }
    public int Checksum { get; set; }
}

public class ClassX : MyBaseClass
{
    // Add the other properties here
}

public class ClassY : MyBaseClass
{
    // Add the other properties here
}

public class ClassZ : MyBaseClass
{
    // Add the other properties here
}

您的辅助方法将具有 MyBaseClass 类型的参数:

public void MyHelperMethod(MyBaseClass obj)
{
    // Do something with obj.Name, obj.Siganture and obj.Checksum
}

将辅助方法放在 MyBaseClass 中也是一个好主意,但没有参数,因为现在它可以直接访问属性:

public abstract class MyBaseClass
{
    public string Name { get; set; }
    public string Signature { get; set; }
    public int Checksum { get; set; }

    public void CreateChecksum() // Your helper method
    {
        Checksum = Name.GetHashCode() ^ Signature.GetHashCode();
    }
}

然后你可以直接从你的对象中调用它:

objA.CreateChecksum();
objB.CreateChecksum();
objB.CreateChecksum();

或者定义一个你的三个类实现的接口:

public interface IMyInterface
{
    string Name { get; set; }
    string Signature { get; set; }
    int Checksum { get; set; }
}

public class ClassX : IMyInterface
{
        public string Name { get; set; }
        public string Signature { get; set; }
        public int Checksum { get; set; }
    // Add the other properties here
}

public class ClassY : IMyInterface
{
        public string Name { get; set; }
        public string Signature { get; set; }
        public int Checksum { get; set; }
    // Add the other properties here
}

public class ClassZ : IMyInterface
{
        public string Name { get; set; }
        public string Signature { get; set; }
        public int Checksum { get; set; }
    // Add the other properties here
}

您的辅助方法将有一个 IMyInterface 类型的参数:

public void MyHelperMethod(IMyInterface obj)
{
    // Do something with obj.Name, obj.Siganture and obj.Checksum
}

您可以像这样调用 MyHelperMethod

MyHelperMethod(objA);
MyHelperMethod(objB);
MyHelperMethod(objC);
于 2011-12-14T19:37:54.183 回答