Con*_*tor 3 c# c++ generics equality type-traits
在C++中,有人可以编写以下代码:
#include <type_traits>
template <typename Type1, typename Type2>
bool TestArgumentTypesOnEquality(Type1 argument1, Type2 argument2)
{
return std::is_same<Type1, Type2>::value;
}
Run Code Online (Sandbox Code Playgroud)
std::is_same类模板检查Type1和Type2(它们是TestArgumentTypesOnEquality函数模板参数的类型)是否相等.
这个C++标准库功能有没有C#泛型模拟?
public static bool TestArgumentTypesOnEquality<Type1, Type2>(Type1 argument1, Type2 argument2)
{
return /* ??? */;
}
Run Code Online (Sandbox Code Playgroud)
当然,只有简单的测试样本.真正的代码更复杂.实际上我只想知道是否可以在C#中测试两个相等的通用类型参数.
C#没有很多编译时功能,例如模板,宏,等等static_assert.C#泛型几乎是运行时.C++模板绝对是编译时间.像编译器时间类型确定机制一样std::is_same,std::enable_if在C#中不起作用.