귀하의 Id 속성에 private
또는 protected
수정자가 있다고 생각합니다. 당신이 public
속성이있는 경우 새 dynamic
키워드를 사용할 수 있습니다
using System;
using System.Reflection;
class Program
{
static void Main()
{
Test t = new Test();
Console.WriteLine(t.GetType().GetProperty("Id1").GetValue(t, null));
Console.WriteLine(t.GetType().GetProperty("Id2").GetValue(t, null));
Console.WriteLine(t.GetType().GetProperty("Id3").GetValue(t, null));
//the next line will throw a NullReferenceExcption
Console.WriteLine(t.GetType().GetProperty("Id4").GetValue(t, null));
//this line will work
Console.WriteLine(t.GetType().GetProperty("Id4",BindingFlags.Instance | BindingFlags.NonPublic).GetValue(t, null));
//the next line will throw a NullReferenceException
Console.WriteLine(t.GetType().GetProperty("Id5").GetValue(t, null));
//this line will work
Console.WriteLine(t.GetType().GetProperty("Id5", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(t, null));
}
public class Test
{
public Test()
{
Id1 = 1;
Id2 = 2;
Id3 = 3;
Id4 = 4;
Id5 = 5;
}
public int Id1 { get; set; }
public int Id2 { private get; set; }
public int Id3 { get; private set; }
protected int Id4 { get; set; }
private int Id5 { get; set; }
}
}
:
static void Main()
{
dynamic s = new Test();
Console.WriteLine(s.Id1);
Console.WriteLine(s.Id3);
}
주 Id2, Id4 and Id5
즉, dynamic
작동하지 않습니다 그럼 당신은 GetProperty
방법의 첫 번째 오버로드를 사용해야합니다 키워드는 공용 프로세서가 없기 때문에 간단하게 아래에 액세스 할 수 있습니다 CALSS에서 때에 프로퍼티 값을 얻기 위해
null은 name "id"로 속성을 검색하지 못했음을 의미한다고 생각합니다. 코드에서 해당 속성은 어떤 모습입니까? – Tigran
어떤 타입이'userData'인지와 그것의 구조를 보여주십시오. –