簡體   English   中英

Reflection.Typeinfo / Reflection.Type沒有GetProperties / GetFields方法

[英]Reflection.Typeinfo/Reflection.Type does not have GetProperties/GetFields method

我正在嘗試為Windows 8.1和Windows Phone 8.1制作Windows通用應用程序。

這是我的問題的一個示例類,我使用int類型作為示例,但無論我使用哪個類,都會出現錯誤:

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace myTtrpgHelper
{
    class testClass
    {
        void testMethod()
        {
            int c = new int();
            Type type = c.GetType();
            TypeInfo typeInfo = IntrospectionExtensions.GetTypeInfo(type);
            PropertyInfo[] p = typeInfo.GetProperties();
            PropertyInfo[] p2 = type.getProperties();

            PropertyInfo[] p3 = typeInfo.GetFields();
            PropertyInfo[] p4 = type.GetFields();
        }
    }
}

GetProperties和GetField都顯示錯誤:

'System.Reflection.TypeInfo' does not contain a definition for 'GetFields' and no extension method 'GetFields' accepting a first argument of type 'System.Reflection.TypeInfo' could be found (are you missing a using directive or an assembly reference?) 

msdn頁面http://msdn.microsoft.com/en-us/library/system.reflection.typeinfo.aspx表示應該支持它,我正在使用visual studio 2013。

您應該使用DeclaredFields屬性來獲取字段,使用DeclaredProperties來獲取屬性。 隨着.NET Framework的發展,Reflection API經歷了一些成長的煩惱。 MSDN信息似乎不准確。 簡而言之,在.NET for Windows Store應用程序中,TypeInfo繼承自MemberInfo而不是Type,因此它不能包含繼承的成員GetFields()GetProperties() 雖然Get *和Declared *成員都存在於完整的Framework中,但對於Windows應用商店應用,您必須使用Declared * API。 本文詳細介紹了各種.NET Framework中Reflection API的不同之處。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM