Mar*_*nro 12 asp.net asp.net-core
我正在尝试将Web应用程序迁移到ASP.Net vNext,最终目的是让它在Linux上运行.
该应用程序有很多反射代码,我必须缺少一些依赖项,因为我在代码上遇到编译错误
Type.IsPrimitive,Type.GetConstructor Type.GetMethod Type.GetTypeArray错误CS1061'Type'不包含'IsPrimitive'的定义,并且没有扩展方法'IsPrimitive'接受类型'Type'的第一个参数可以找到(你是否遗漏了) using指令或程序集引用?)
错误CS1061'Type'不包含'GetMethod'的定义,也没有扩展方法'GetMethod'接受类型'Type'的第一个参数(你是否缺少using指令或汇编引用?)
错误CS1061'Type'不包含'GetProperties'的定义,也没有扩展方法'GetProperties'接受类型'Type'的第一个参数(你是否缺少using指令或汇编引用?)
错误CS1061'Type'不包含'GetInterface'的定义,并且没有扩展方法'GetInterface'接受类型'Type'的第一个参数可以找到(你是否缺少using指令或汇编引用?)
我的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"
}
Run Code Online (Sandbox Code Playgroud)
以下编译在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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
cyg*_*nim 21
如果您正在使用aspnetcore .IsPrimitive可用,但不是Type的成员.你可以在TypeInfo下找到它,可以通过调用GetTypeInfo()Type 的方法来访问它.在您的示例中,它将是:
lStringType.GetTypeInfo().IsPrimitive
Run Code Online (Sandbox Code Playgroud)
Type.GetMethod()也可用,但您需要System.Reflection.TypeExtensions在project.json文件中引用该包.
Type.GetTypeArray() 缺少,但您可以轻松编写一个简单的linq查询来检索数组中的成员类型数组.
Type.GetInterface()不包含,但再次使用System.Reflection.TypeExtensions将公开另一种方法,该方法Type[]为指定的类型生成所有已实现的接口.
Type[] types = Type.GetInterfaces()
Run Code Online (Sandbox Code Playgroud)
Type.GetProperties()可以通过System.Reflection.TypeExtensions图书馆再次使用.
| 归档时间: |
|
| 查看次数: |
3967 次 |
| 最近记录: |