Nat*_*lor 1 .net c# reflection types
我需要检测一个类型的对象是否IDictionary<string, string>是一个,IDictionary<,>并且我无法提出正确的比较逻辑.
我尝试过以下方法:
typeof(IDictionary<string, string>)
.GetInterface(typeof(IDictionary<,>).Name);
Run Code Online (Sandbox Code Playgroud)
和
typeof(IDictionary<string, string>)
.GetGenericTypeDefinition()
.GetInterface(typeof(IDictionary<,>).Name);
Run Code Online (Sandbox Code Playgroud)
调用typeof(Dictionary<string,string>).GetInterface(comparisonType.Name)返回预期的非null结果,但如果我在IDictionary<string,string>类型上进行比较,则GetInterface()返回null.同样,在GenericTypeDefinition上进行比较也会返回null.
typeof(IDictionary<string, string>).GetGenericTypeDefinition() == typeof(IDictionary<,>)
Run Code Online (Sandbox Code Playgroud)