KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
When playing around with reflection in the new .NET Framework 4.5, I encountered a strange behavior that I found quite unexpected. The namespace System.Reflection provides some new extension methods for exploiting Type objects. Two of those are GetRuntimeProperty(string name) and GetRuntimeProperties(). Now imagine that you have a simple object with an internal property. public class ObjectBase { protected int Id { get; set; } public string Name { get; set; } } And you now try to exploit this type. var properties = typeof(ObjectBase).GetRuntimeProperties(); // properties.Count = 2 var idProperty = typeof(ObjectBase).GetRuntimeProperty("Id"); var nameProperty = typeof(ObjectBase).GetRuntimeProperty("Name"); // idProperty = null // nameProperty = System.String Name As expected the properties object holds two property definition for the Id and Name property defintions and the nameProperty holds the Name property definition. What was not expected was the idProperty object to be null... Coming from the .NET Framework, I guess this was intended by Microsoft architects but I must say it does not seem like something you would really expect to happen. I do believe such similar methods should behave the same but it seems that GetRuntimeProperty filters on public properties where GetRuntimeProperties apply no filters. Does anybody have a reasonable explanation on why do Microsoft decided that those similar methods should have different behaviors? A design error? Thanks.
Tags (comma-separated)
Save Edits
Cancel