该应用程序有很多反射代码,我必须缺少一些依赖关系,因为我正在得到如此代码的编译错误
Type.IsPrimitive,Type.GetConstructor Type.getmethod Type.GetTypeArray
Error CS1061 ‘Type’ does not contain a deFinition for ‘IsPrimitive’ and no extension method ‘IsPrimitive’ accepting a first argument of type ‘Type’ Could be found (are you missing a using directive or an assembly reference?)Error CS1061 ‘Type’ does not contain a deFinition for ‘getmethod’ and no extension method ‘getmethod’ accepting a first argument of type ‘Type’ Could be found (are you missing a using directive or an assembly reference?)
Error CS1061 ‘Type’ does not contain a deFinition for ‘GetProperties’ and no extension method ‘GetProperties’ accepting a first argument of type ‘Type’ Could be found (are you missing a using directive or an assembly reference?)
Error CS1061 ‘Type’ does not contain a deFinition for ‘GetInterface’ and no extension method ‘GetInterface’ accepting a first argument of type ‘Type’ Could be found (are you missing a using directive or an assembly reference?)
我的project.json文件中有以下依赖关系
"frameworks" : { "aspnetcore50" : { "dependencies": { "System.Runtime": "4.0.20-beta-22416","System.Linq": "4.0.0.0-beta-22605","System.Reflection": "4.0.10.0-beta-22605","System.Reflection.Primitives": "4.0.0.0-beta-22605","System.Runtime.Extensions": "4.0.10.0-beta-22605","System.Reflection.Extensions": "4.0.0.0-beta-22605" }
以下编译在VS 2013和.Net 4.5下,但不会在VS 2015中使用上述依赖项进行编译
using System; using System.Reflection; namespace Project1 { public class Class1 { public Class1() { Type lBaseArrayType = typeof(Array); Type lStringType = typeof(string); string[] lStringArray = new string[1]; if (lStringType.IsPrimitive) { } ConstructorInfo lConstructor = lStringType.GetConstructor(new Type[0]); MethodInfo lMethod = lStringType.getmethod("Equals"); Type[] lTArray = Type.GetTypeArray(lStringArray); PropertyInfo[] lProps = lStringType.GetProperties(); } } }
解决方法
lStringType.GetTypeInfo().IsPrimitive
Type.getmethod()也可用,但您需要在project.json文件中引用System.Reflection.TypeExtensions包.
Type.GetTypeArray()缺少,但您可以轻松地编写一个简单的linq查询来检索数组中的成员类型数组.
Type.GetInterface()不包括在内,但是再次使用System.Reflection.TypeExtensions将会公开另一种为指定类型生成所有实现的接口的Type []的方法.
Type[] types = Type.GetInterfaces()
Type.GetProperties()可以通过System.Reflection.TypeExtensions库再次可用.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。