Question

I have several objects with a few properties that are common to all of them. For example:

Object A is of type X and it has 10 properties

Object B is of type Y and it has 15 properties

Object C is of type Z and it has 7 properties

All of these objects have "name", "signature" and "checksum" properties in common. I'm trying to create a static helper method that can accept an object of any type that contains "name", "signature" and "checksum" properties. Is that possible or do I actually need three different methods (one to accept an object of each type)?

EDIT - For what it's worth, I failed to mention that these objects are exposed to me via a web service.

Was it helpful?

Solution

I am going to assume that since these obejcts are exposed via a web service, then you have no control over the definition of the object. They are what they are and you can't have them inherit from a common base class, or interface. With that constraint to the problem, you really have only two options.

If you are using C# 4.0 or later you can use the new dynamic type. This basically is an object reference that does not do any type evalutation until run time. So if a property or method you use on the dynamic type does not exist your going to get a run time error rather than a error during compliation.

The other otpion is going to simply take a reference to type Object and use reflection to manipulate properties and methods. A lot of potential ugly there.

I you are not using C# 4+ then I think I would go with the three seperate methods. While you might duplicate some code, I would rather have that than a bunch of complex hard to follow reflection calls that you would have to use in c# 3.5-

OTHER TIPS

You should move those properties to a common base class or interface and use that.

You have two good options. The first is inheritance:

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;
}

This can be powerful since you can make those properties virtual, and each class can override to handle them differently.

The second option is using an interface:

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; } }
}

It is possible... if you define a common base class containing the common methods, and make Objects A, B, and C subclasses of the base. Your static helper method can then use the base class as its parameter type, and any of the sub-types can be passed into it.

For another option there is also of dynamic typing. Although this option should work for you, I would definitely try to use an interface or base class as the others have mentioned.

an interface is your best option I believe,

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;}
  ...
}

your helper method then, would take ISomeGoodNameForCommonProperies

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

inheritance would work of course, however I would avoid base classes unless it makes sense for the objects you are trying to create. the question you should be asking yourself, can X, Y, and Z be defined as some type of a different object O? if so go ahead and create O and inherent from it. If these 3 properties you have in common are not enough to define a logical entity but they need to be grouped together for practical reason, an interface is your best option.

You can use inheritance.

There are two approaches to this problem:

Create a base class that has all these common properties and derive the others from it.

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
}

Your helper method will have a parameter of type MyBaseClass:

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

It would also be a good idea to place the helper method in MyBaseClass, but without parameters, since now it can access the properties directly:

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();
    }
}

Then you can call it directly from your objects:

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

Or define an interface that your three classes implement:

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
}

Your helper method will have a parameter of type IMyInterface:

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

You can call MyHelperMethod like this

MyHelperMethod(objA);
MyHelperMethod(objB);
MyHelperMethod(objC);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top