有人可以在 C++ 中编写以下代码:
#include <type_traits>
template <typename Type1, typename Type2>
bool TestArgumentTypesOnEquality(Type1 argument1, Type2 argument2)
{
return std::is_same<Type1, Type2>::value;
}
std::is_same
类模板检查 Type1
和 Type2
(它们是 TestArgumentTypesOnEquality
函数的类型模板参数)是相等的。
是否有此 C++ 标准库功能的任何 C# 泛型模拟?
public static bool TestArgumentTypesOnEquality<Type1, Type2>(Type1 argument1, Type2 argument2)
{
return /* ??? */;
}
当然只有简单的测试样本。实际代码更复杂。事实上,我只想知道是否可以在 C# 中测试两个泛型类型参数是否相等。
最佳答案
C# 没有很多编译时特性,例如模板、宏、static_assert
。 C# 泛型几乎是运行时的。 C++ 模板绝对是编译时间。 std::is_same
、std::enable_if
等编译器时间类型确定机制在 C# 中不起作用。
关于c# - 是否有 C++ std::is_same 类模板的 C# 类似物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37702714/