Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2006

Tip: Looking for answers? Try searching our database.

typeof keyword for properties, fields, methods

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Amid - 11 Oct 2006 12:04 GMT
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)

Is there any problems that prevents Microsoft from implementing this?

I would appreciate any thoughts.
Thanks in advance.
Patrick Steele - 11 Oct 2006 14:11 GMT
> Why don't Microsoft implement some sort of "typeof" for properties, fields,
> methods etc?
[quoted text clipped - 3 lines]
>
> PropertyInfo pi = propertyof(MyClass.MyProperty)

What are you trying to accomplish?  Do you want to get a PropertyInfo
without using reflection?

Signature

Patrick Steele
http://weblogs.asp.net/psteele

Amid - 12 Oct 2006 14:44 GMT
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.
Mattias Sjögren - 12 Oct 2006 22:40 GMT
>Why don't Microsoft implement some sort of "typeof" for properties, fields,
>methods etc?
[quoted text clipped - 5 lines]
>
>Is there any problems that prevents Microsoft from implementing this?

The suggestion has come up before, and I believe the C# team has said
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

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.