gpt4 book ai didi

java - .Net 等同于 Java 类型的 Class<>?

转载 作者:太空狗 更新时间:2023-10-29 22:58:27 26 4
gpt4 key购买 nike

我是 .NET 专家,所以让我先声明我对一些 Java 概念的理解 - 如果我错了请纠正我。

Java 泛型支持有界通配符的概念:

class GenericClass< ? extends IInterface> { ... }

...类似于 .NET where限制:

class GenericClass<T> where T: IInterface { ... }

Java 的 Class 类描述了一种类型,大致等同于 .NET Type

到目前为止,还不错。但是我找不到与 Java 通用类型 Class<T> 足够接近的等价物。其中 T 是有界通配符。这基本上对 Class 的类型施加了限制。代表。

让我举个 Java 的例子。

String custSortclassName = GetClassName(); //only known at runtime, 
// e.g. it can come from a config file
Class<? extends IExternalSort> customClass
= Class.forName("MyExternalSort")
.asSubclass(IExternalSort.class); //this checks for correctness

IExternalSort impl = customClass.newInstance(); //look ma', no casting!

我在 .NET 中能得到的最接近的是这样的:

String custSortclassName = GetClassName(); //only known at runtime, 
// e.g. it can come from a config file

Assembly assy = GetAssembly(); //unimportant

Type customClass = assy.GetType(custSortclassName);
if(!customClass.IsSubclassOf(typeof(IExternalSort))){
throw new InvalidOperationException(...);
}
IExternalSort impl = (IExternalSort)Activator.CreateInstance(customClass);

Java 版本对我来说看起来更干净。有没有办法改进 .NET 对应物?

最佳答案

System.Type 使用扩展方法和自定义包装类,您可以非常接近 Java 语法。

注意: Type.IsSubclassOf 不能用于测试类型是否实现接口(interface) - 请参阅 MSDN 上的链接文档。可以使用 Type.IsAssignableFrom 相反 - 请参阅下面的代码。

using System;

class Type<T>
{
readonly Type type;

public Type(Type type)
{
// Check for the subtyping relation
if (!typeof(T).IsAssignableFrom(type))
throw new ArgumentException("The passed type must be a subtype of " + typeof(T).Name, "type");

this.type = type;
}

public Type UnderlyingType
{
get { return this.type; }
}
}

static class TypeExtensions
{
public static Type<T> AsSubclass<T>(this System.Type type)
{
return new Type<T>(type);
}
}

// This class can be expanded if needed
static class TypeWrapperExtensions
{
public static T CreateInstance<T>(this Type<T> type)
{
return (T)Activator.CreateInstance(type.UnderlyingType);
}
}

使用接口(interface)差异的进一步改进

(应仅在评估性能后在生产代码中使用。可以通过使用(并发!)缓存字典来改进 ConcurrentDictionary<System.Type, IType<object>)

使用 Covariant type parameters 、C# 4.0 引入的一个特性,以及一个附加类型 interface IType<out T> ,这Type<T>实现,可以使类似以下的事情成为可能:

// IExternalSortExtended is a fictional interface derived from IExternalSort
IType<IExternalSortExtended> extendedSort = ...
IType<IExternalSort> externalSort = extendedSort; // No casting here, too.

甚至可以这样做:

using System;

interface IType<out T>
{
Type UnderlyingType { get; }
}

static class TypeExtensions
{
private class Type<T> : IType<T>
{
public Type UnderlyingType
{
get { return typeof(T); }
}
}

public static IType<T> AsSubclass<T>(this System.Type type)
{
return (IType<T>)Activator.CreateInstance(
typeof(Type<>).MakeGenericType(type)
);
}
}

static class TypeWrapperExtensions
{
public static T CreateInstance<T>(this IType<T> type)
{
return (T)Activator.CreateInstance(type.UnderlyingType);
}
}

这样就可以(明确地)在不相关的接口(interface)之间转换 InterfaceAInterfaceB喜欢:

var x = typeof(ConcreteAB).AsSubclass<InterfaceA>();
var y = (IType<InterfaceB>)x;

但这有点违背了练习的目的。

关于java - .Net 等同于 Java 类型的 Class<>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14237113/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com