-1
Java代码差异在Java中对C#
public abstract class A<T extends AA> extends B
{
public A()
{
Type type = this.getClass().getGenericSuperclass();
ParameterizedType parametrizedType = (ParameterizedType) type;
Type[] fieldArgTypes = parametrizedType.getActualTypeArguments();
parameterType = (Class<T>) fieldArgTypes[0];
}
}
在C#.NET CORE我写了这个:
public abstract class A<T> : B where T: class
{
public A()
{
Type type = GetType().GetGenericTypeDefinition();
parameterType = type.GenericTypeArguments[0];
}
}
但是当我运行程序我有错误:This operation is only valid on generic types.
我有不知道如何解决这个问题。我想要实现Java代码中的一样。
应该在类型实例上调用'GetType()'。该行是否编译'GetType()。GetGenericTypeDefinition();'? – Rahul
@Rahul和Java代码你看到类型?我需要在C#中编写相同的代码。是的 – Bubuu
不清楚你想在这里做什么,但你在寻找'this.GetType()。GetGenericArguments()'? – DavidG