我遇到了泛型的奇怪行为.下面是我用于测试的代码.
public static class Program
{
public static void Main()
{
Type listClassType = typeof(List<int>).GetGenericTypeDefinition();
Type listInterfaceType = listClassType.GetInterfaces()[0];
Console.WriteLine(listClassType.GetGenericArguments()[0].DeclaringType);
Console.WriteLine(listInterfaceType.GetGenericArguments()[0].DeclaringType);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
System.Collections.Generic.List`1[T]
System.Collections.Generic.List`1[T]
Run Code Online (Sandbox Code Playgroud)
我发现第二个Console.WriteLine调用显示一个类而不是一个接口是非常奇怪的,因为我使用泛型类型定义.这是正确的行为吗?
我正在尝试在我的编译器中实现泛型类型推断.假设我有以下代码.
public static class GenericClass
{
public static void GenericMethod<TMethodParam>(IList<TMethodParam> list) { }
}
Run Code Online (Sandbox Code Playgroud)
我想将此方法称为如下:
GenericClass.GenericMethod(new List<int>());
Run Code Online (Sandbox Code Playgroud)
为了检查推理的可能性,我必须比较方法签名中的类型和传递的参数类型.但是下面的代码返回false.
typeof(GenericClass).GetMethods()[0].GetParameters()[0].ParameterType == listInterfaceType;
Run Code Online (Sandbox Code Playgroud)
我是否应该始终使用Type.GetGenericTypeDefinition进行此类比较?
我需要创建两个严格交替的线程.这是我使用的示例代码:
#include <Windows.h>
#include <iostream>
using std::cout;
using std::endl;
HANDLE g_hMutex1;
HANDLE g_hMutex2;
DWORD WINAPI ThreadFunc1(LPVOID lpParam);
DWORD WINAPI ThreadFunc2(LPVOID lpParam);
int main(void)
{
int nCalcNumber = 10;
DWORD dwThreadId;
HANDLE pThreadHandles[2];
g_hMutex1 = CreateMutex(NULL, FALSE, NULL);
g_hMutex1 = CreateMutex(NULL, FALSE, NULL);
pThreadHandles[0] = CreateThread(
NULL,
0,
ThreadFunc1,
static_cast<void*>(&nCalcNumber),
0,
&dwThreadId);
pThreadHandles[1] = CreateThread(
NULL,
0,
ThreadFunc2,
static_cast<void*>(&nCalcNumber),
0,
&dwThreadId);
WaitForMultipleObjects(2, pThreadHandles, TRUE, INFINITE);
CloseHandle(pThreadHandles[0]);
CloseHandle(pThreadHandles[1]);
CloseHandle(g_hMutex1);
CloseHandle(g_hMutex2);
return 0;
}
DWORD WINAPI ThreadFunc1(LPVOID lpParam)
{
int* nCalcNumber = static_cast<int*>(lpParam); …Run Code Online (Sandbox Code Playgroud) 我应该使用哪个 API?哪一个更有前途?我的主要目标是生成 ODF 文档。支持 ODF 1.2 对我来说很重要。我想使用 C++ 和 CLI。