Method can take two objects of different class?
I interpreted this as the following:
I need a method that can take objects that appear the same, regardless of implementation
Like I mentioned in my comment, you should use an interface
-- consider the following:
public interface ICommonObject
{
int Id { get; }
string Name { get; }
}
public class ObjectOne : ICommonObject
{
int Id { get; }
string Name { get; }
public ObjectOne(int id, string name)
{
Id = id;
Name = name;
}
}
public class ObjectTwo : ICommonObject
{
int Id { get; }
string Name { get; }
public ObjectTwo(int id, string name)
{
Id = id;
Name = name;
}
}
Then in your method, you take the ICommonObject
interface as the parameter -- that way either implementation can be passed into it as an argument. Like so:
public class CommonLogic
{
public void ActOnCommonObject(ICommonObject commonObject)
{
// TODO: do somthing with "commonObject"
}
}
Now you could have an instance of either common object, so long as it implements that interface it is valid as an argument. The following code would be fine:
{
var logic = new CommonLogic();
var one = new ObjectOne();
var two = new ObjectTwo();
logic.ActOnCommonObject(one);
logic.ActOnCommonObject(two);
}
You are left with an encapsulated snippet of logic that can act agnostic to the specific implementation, that can be easily unit tested.
For more details on interface
implementation check out this MSDN article.