Jerry Jerry -4 years ago 94
C# Question

In C#, how do I use generics with two different types?

I have some classes that are created and maintained by Entity Framework. These classes represent tables in my DB. In two of these tables, I have very similar fields. For example (pseudo-objects):

public class RandomObject1
{
int Identifier { get; set; }
int ObjectType { get; set; }
int SourceID { get; set; }
string OriginationPont { get; set; }
string PhoneNumber { get; set; }
decimal Cost { get; set; }

int OtherThing1 { get; set; }
int OtherThing2 { get; set; }
int OtherThing3 { get; set; }
}

public class RandomObject2
{
int Identifier { get; set; }
int ObjectType { get; set; }
int SourceID { get; set; }
string OriginationPont { get; set; }
string PhoneNumber { get; set; }
decimal Cost { get; set; }

double CashValue1 { get; set; }
decimal Snowman2 { get; set; }
int BigFurryTree3 { get; set; }
}


Note that the first few fields of these two objects are the same, and the processes for populating those fields are also the same. Normally in these situations I would have an interface that declares just the first few fields so that I can pass this object as an interface to various processes.

However, in this case, I don't control the code that builds these classes, and really don't want to have to edit the resulting .cs files from the Entity Framework every time it is regenerated.

I was wondering if there is a slick way that I am missing to use generics to do something like the following:

// This method will populate SourceID, OriginationPoint, PhoneNumber and Cost
public void GenerateOriginationInformation<T>(ValidationInformation info, T objectToManipulate) where T : RandomObject1 || RandomObject2
{
objectToManipulate.SourceID = GenerateSourceID(info);
objectToManipulate.OriginationPoint = GenerateOriginationPoint(info);
objectToManipulate.PhoneNumber = FindPhoneNumberByOrigination(info);
objectToManipulate.Cost = DetermineCostBySourceAndOrigination(info);
}


Right now, I have to build an entire object/layer that will populate and return the correct object, but results in me doing most of the code for these things twice!

public void GenerateOriginationInformation(ValidationInformation into, RandomObject1 objectToManipulate)
{
objectToManipulate.SourceID = GenerateSourceID(info);
objectToManipulate.OriginationPoint = GenerateOriginationPoint(info);
objectToManipulate.PhoneNumber = FindPhoneNumberByOrigination(info);
objectToManipulate.Cost = DetermineCostBySourceAndOrigination(info);
}

public void GenerateOriginationInformation(ValidationInformation into, RandomObject2 objectToManipulate)
{
objectToManipulate.SourceID = GenerateSourceID(info);
objectToManipulate.OriginationPoint = GenerateOriginationPoint(info);
objectToManipulate.PhoneNumber = FindPhoneNumberByOrigination(info);
objectToManipulate.Cost = DetermineCostBySourceAndOrigination(info);
}


At first, this doesn't look too bad, but this code is highly over-simplified for the purposes of explanation and brevity. Is there a cleaner way to use generics to get the two methods to work as one since I can't implement an interface?

Answer Source

I don't control the code that builds these classes, and really don't want to have to edit the resulting .cs files from the Entity Framework every time it is regenerated

answer in comment: "it is EDMX"

The generated classes from your EDMX designer are partial by default (no additional work necessary by you) so you can create a code file next to the generated files with a partial as well in which you make the type implement an interface.

Generated class

public partial class RandomObject1

Your code file placed in the same project

public partial class RandomObject1 : ICommonInterface
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download