具有泛型类型检查的泛型方法调用
Generic method calls with generic type checks
我正在尝试概括一些构建器重复方法调用。
我有一些实体,如果它们已经实现了一些接口,我会尝试实现调用标准方法。
这是我试图在代码中实现的目标:
void MainMethod(){
...
HandleStandards<MyClass>(builder);
...
}
void HandleStandards<T>(..builder..) where T: class, IEntity
{
// of course my problem is with here. because T is not suitable to pass to other methods
if(T is IIdentifiable){
HandleIdentifiable<T>(builder);
}
if(T is ITrackable){
HandleTrackable<T>(builder);
}
}
void HandleIdentifiable<T>(..builder..) where T: class, IEntity, IIdentifiable
{
...
}
void HandleTrackable<T>(..builder..) where T: class, IEntity, ITrackable
{
...
}
有没有办法对泛型类型进行类型检查并将其传递给另一个泛型方法?
你可以做到这一点,使用一些反射魔法,或者,如果你的 HandleStandards
有一个类型为 T
的参数,你可以更容易地做到这一点。所以首先,让我们看看如果 builder
的类型是 T
:
怎么办
void HandleStandards<T>(T builder) where T: class, IEntity
{
if (builder is IIdentifiable identifiable)
{
HandleIdentifiable(identifiable)
}
// You'd go on like this
}
如果你没有T
类型的参数,我们将不得不使用一些反射魔法来做到这一点,但它不是很多,这里是:
void Handle Standards<T>() where T : class, IEntity
{
if (typeof(T).IsAssignableTo(typeof(IIdentifiable)))
{
// You'll probably have to use some BindingFlags
// Here I'm assuming that 'HandleIdentifiable' is private and static
// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.bindingflags?view=net-6.0
var method = typeof(TypeContainingTheseMethods).GetMethod(
nameof(HandleIdentifiable),
BindingFlags.NonPublic | BindingFlags.Static
);
// If the method is *not* static, you'll need to pass the instance
// on which to call the method on as the first parameter instead of null
// Plus any additional parameters you may have inside the object array
// in the same order as the method declares them
method.MakeGenericMethod(typeof(IIdentifiable))
.Invoke(null, new object[] { builder });
}
}
我正在尝试概括一些构建器重复方法调用。 我有一些实体,如果它们已经实现了一些接口,我会尝试实现调用标准方法。 这是我试图在代码中实现的目标:
void MainMethod(){
...
HandleStandards<MyClass>(builder);
...
}
void HandleStandards<T>(..builder..) where T: class, IEntity
{
// of course my problem is with here. because T is not suitable to pass to other methods
if(T is IIdentifiable){
HandleIdentifiable<T>(builder);
}
if(T is ITrackable){
HandleTrackable<T>(builder);
}
}
void HandleIdentifiable<T>(..builder..) where T: class, IEntity, IIdentifiable
{
...
}
void HandleTrackable<T>(..builder..) where T: class, IEntity, ITrackable
{
...
}
有没有办法对泛型类型进行类型检查并将其传递给另一个泛型方法?
你可以做到这一点,使用一些反射魔法,或者,如果你的 HandleStandards
有一个类型为 T
的参数,你可以更容易地做到这一点。所以首先,让我们看看如果 builder
的类型是 T
:
void HandleStandards<T>(T builder) where T: class, IEntity
{
if (builder is IIdentifiable identifiable)
{
HandleIdentifiable(identifiable)
}
// You'd go on like this
}
如果你没有T
类型的参数,我们将不得不使用一些反射魔法来做到这一点,但它不是很多,这里是:
void Handle Standards<T>() where T : class, IEntity
{
if (typeof(T).IsAssignableTo(typeof(IIdentifiable)))
{
// You'll probably have to use some BindingFlags
// Here I'm assuming that 'HandleIdentifiable' is private and static
// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.bindingflags?view=net-6.0
var method = typeof(TypeContainingTheseMethods).GetMethod(
nameof(HandleIdentifiable),
BindingFlags.NonPublic | BindingFlags.Static
);
// If the method is *not* static, you'll need to pass the instance
// on which to call the method on as the first parameter instead of null
// Plus any additional parameters you may have inside the object array
// in the same order as the method declares them
method.MakeGenericMethod(typeof(IIdentifiable))
.Invoke(null, new object[] { builder });
}
}