I have an abstract base class with generics, I will simplify it for this question:
public abstract class DBBaseTable<T>
{
public T Id { get; set; }
public byte[] RowVersion { get; set; }
}
Now, I can have classes like: Person : DBBaseTable<int> or Animal: DBBaseTable<Guid>
I am trying to avoid reflection in order to access the property RowVersion in a part of the code where I simply do not know the T Type.
var memberInfo = oldObject.GetType().BaseType;
if (memberInfo != null
&& memberInfo.IsGenericType
&& memberInfo.GetGenericTypeDefinition() == typeof(DBBaseTable<>)
)
{
var isCorrectRowVersion = (oldObject as DBBaseTable<object>).RowVersion ==
(objToUpdate as DBBaseTable<object>).RowVersion;
//TODO....
}
The conditional IF condition is working fine... but then when I try to use the as operator I always need to specify the T type... I have tried to use "object" but it is not working... and I simply do not know if I am dealing with int, guid or some other type....
I know how to solve this with reflection, but I would like to know if there is an alternative?