GetProperty方法返回null C#.Net [英] GetProperty Method returns null C# .Net

查看:351
本文介绍了GetProperty方法返回null C#.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一下,为什么System.Type中的GetProperty方法对于声明为内部但适用于公共的属性返回null。

Can someone please explain, why does the GetProperty method in System.Type returns null for properties that are declared as 'internal' but works for 'public'.

internal class Test{      
  public string ALocal { get; set; }
  internal string SLocal { get; set; }}

var test = new Test();
var testType = test.GetType();

var aProp = testType.GetProperty("ALocal"); => returns string Type
var sProp = testType.GetProperty("SLocal"); => returns null

我了解内部修饰符或公共修饰符之间的区别。

I understand differences between internal or public modifiers.

推荐答案

GetProperty方法默认情况下仅返回公共属性。
您应包括以下标志

GetProperty Method returns only public properties by default. You should include following flags

BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static 

获取内部类型

MSDN:
https:// msdn .microsoft.com / en-us / library / zy0d4103(v = vs.110).aspx

这篇关于GetProperty方法返回null C#.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆