[英]C# as operator on Type with generics but irrelevant <T>
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>
现在,我可以有这样的类:
Person : DBBaseTable<int>
或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.我试图避免反射,以便在我根本不知道
T
类型的代码的一部分中访问属性 RowVersion。
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....条件 IF 条件工作正常......但是当我尝试使用
as
运算符时,我总是需要指定T
类型......我尝试使用“对象”但它不起作用......我只是不知道我是在处理 int、guid 还是其他类型的......
I know how to solve this with reflection, but I would like to know if there is an alternative?我知道如何通过反射解决这个问题,但我想知道是否有替代方案?
Move the RowVersion
into a base class.将
RowVersion
移动到基类中。
public abstract DBBaseTable
{
public byte[] RowVersion { get; set; }
}
public abstract class DBBaseTable<T> : DBBaseTable
{
public T Id { get; set; }
}
Another option is to declare a base interface with RowVersion
property and implement it in DBBaseTable<T>
base class另一种选择是声明一个具有
RowVersion
属性的基接口并在DBBaseTable<T>
基类中实现它
public interface IDBTable
{
byte[] RowVersion { get; set; }
}
public abstract class DBBaseTable<T> : IDBTable
{
public T Id { get; set; }
public byte[] RowVersion { get; set; }
}
public class Person : DBBaseTable<int>
{
//rest of logic
}
Then you can access a RowVersion
property without a cast然后您无需
RowVersion
即可访问RowVersion
属性
var isCorrectRowVersion = oldObject.RowVersion == objToUpdate.RowVersion;
With interfaces you are allowed to use covariance and contravariance in generic types.使用接口,您可以在泛型类型中使用协变和逆变。
Also, you can implement multiple interfaces in one class, but inherit only one base class in DBBaseTable<T>
.此外,您可以在一个类中实现多个接口,但在
DBBaseTable<T>
只继承一个基类。 If you'll need to extend it in future, using an interface is preferable, IMO.如果您将来需要扩展它,最好使用接口,IMO。 But final decision depends on your needs.
但最终决定取决于您的需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.