I am trying to write a Method that uses the Name
property of diffrent classes and does some logic with it.
In my exampel to keep it simple i will restrict myself to simply returning the Name
value:
class Dog
{
public string Name { get; set; }
}
class Human
{
public string Name { get; set; }
}
/*
...
*/
public string GetNameFromNameProperty(object obj)
{
return obj.Name;
}
Sadly the classes are not inherting from a parent class that has the Name
property. Furthermore it is not possible to implement that or add an interface.
Short Recap:
Is it possible to write a generic Method that uses a Name
property without being sure that the class has implemented that property?