|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
typeof keyword for properties, fields, methodsWhy don't Microsoft implement some sort of "typeof" for properties, fields,
methods etc? For example, lets suppose we have "propertyof" keyword, then we can do the following: PropertyInfo pi = propertyof(MyClass.MyProperty) Is there any problems that prevents Microsoft from implementing this? I would appreciate any thoughts. Thanks in advance. In article <FAB852C2-A5E8-48A5-807A-9F14DCC8B***@microsoft.com>,
A***@discussions.microsoft.com says... > What are you trying to accomplish? Do you want to get a PropertyInfo > Why don't Microsoft implement some sort of "typeof" for properties, fields, > methods etc? > > For example, lets suppose we have "propertyof" keyword, then we can do the > following: > > PropertyInfo pi = propertyof(MyClass.MyProperty) without using reflection? I'll try to explain. Letts suppose we have the following method to fetch data
from the database: BizEntity[] e = bll.ExecuteQuery("type of the business object", " property name of the business object", ....); It takes as first parameter name of the business logic entity which objects I want to get, and other parameters are names of the properties I want to be loaded of the fetched objects (suppose we have a delay loaded business objects). I can rewrite this snippet: BizEntity[] e = bll.ExecuteQuery(typeof(BizCustomer), " property name of the BizCustomer", ....); Now if for some reason business entity "BizCustomer" will be deleted or renamed the error will be show at compile time. This is really good ability, espessially for the large systems with lots of business objects. Now suppose we have "propertyof" keyword, then the snippet will be like this: BizEntity[] e = bll.ExecuteQuery(typeof(BizCustomer), propertyof(BizCustomer.Name), ....); And the compiler will show us an error even if the property "Name" of the "BizCustomer" object will be renamed or deleted. As you see I cannot use reflection because I will need to use string names of the properties anyway so it will not help. And I cannot see why Microsoft havn't provided such possibility. >Why don't Microsoft implement some sort of "typeof" for properties, fields, The suggestion has come up before, and I believe the C# team has said>methods etc? > >For example, lets suppose we have "propertyof" keyword, then we can do the >following: > >PropertyInfo pi = propertyof(MyClass.MyProperty) > >Is there any problems that prevents Microsoft from implementing this? that it's on their list of things to consider for future versions. I can certainly see some challenges though. For example, you need to find a way to deal with overload resolution for methods and indexers. I'm sure they could implement it, but I don't know if the feature is asked for often enough to justify it. Personally I'd settle for an operator that "string-ifies" (i.e. compiles down to a string literal) an identifier while still verifying that the identifier is valid at compile time. It would be very useful when throwing ArgumentExceptions and the like. Mattias -- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup. |
|||||||||||||||||||||||