English 中文(简体)
为什么这个代码告诉我 找不到类型或命名空间?
原标题:Why does this code tell me the type or namespace could not be found?

根据以下编码:

var property = typeof(TEntity).GetProperty("Id");
var temp = (property.PropertyType)id;

VS 在第二行的属性下划线, 并告诉我找不到类型或名称空间 。 为什么? 我试过在顶部使用且没有使用 < code> 系统。 reflection , 并获得相同的结果 。

我通过使用 Convert. changeType (id, propertyType) 来绕过它工作,但我好奇C# spec( C# spec) 如何使先前的代码无效 。

最佳回答

It expects a type (not variable of type System.Type but actual type) between the parentheses, but such type (property.PropertyType) doesn t exist. To prove this add this to your code:

public class property
{
    public class PropertyType
    {

    }
}

现在,编译者会满意的, 但这可能不是你想做的。

问题回答

您正试图通过执行时才知道的东西 < em> 播送 < / em > 。 C# 不像这样工作 。 投影片的类型部分不是“ 用于评价 < code> Type 引用的表达式 ” - 它是 < em> 类型或类型参数 的名称 。

不清楚您试图在这里实现什么目标, 但是您将无法通过这样的铸造来完成它 。 您期望 < code> temp 的编译时间类型是什么? 您是否意识到 < code> var 仅仅是编译时间类型推理, 不是动态打字?

如果你能告诉我们更多关于你试图实现的目标的信息,我们也许能够帮助你更多。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签