Click here to Skip to main content
15,891,621 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
See more:
Hello...

I have been working with the .NET development environments ever since it was commercially released in 2001.

I am fluent both in VB.NET and C#, though my preferred language has always been VB.NET (chalk it up to my many years as a DBase developer).

In any event, who do language designers make the lives of us developers and engineers so god awful difficult?

Recently, I have been developing a complex extension for a new commercial application I have been developing. This paid-for extension will provide the application with a number of security features, including application password processing.

As a result, this extension, which will be loaded at run-time (if a user purchases it) will provide the password entry form as the initial aspect of the application startup (and the user has previously set up a password in the application's database).

OK, good so far. The password entry form has a property in it, which will be accessed by Reflection, which will hold the entered password. This way, the parent\host application can access the data in a more approriate way instead of this property simply being a public string.

So how do we have to get access to such data?

>>>
VB
' retrieve value of window's "ENTERED_PASSWORD" property
loPropertyInfo = loWindow.GetType().GetProperty("ENTERED_PASSWORD")
lsEnteredPassword = DirectCast(loPropertyInfo.GetValue(loWindow, Nothing), String)
<<<

This solution took me close to 3 hours to find; at least for the one that I found actually works.

The question is, why does one have to again do a GetValue call of the PropertyInfo class, whic then has to onec again refer the calling method back to the original source of where the property is defined (in this case "ENTERED_PASSWORD" in the password entry form, which is defined as "loWindow")?

Of all the stupid things that Microsoft has come up with this has got to be one the dumbest.

One would think that once you get the PropertyInfo object you could simply access the named property value from it. And even though you can list out the named property information, getting a value is another gyration completely.

Any thoughts on this would be most appreciative as I hate it when I am looking for something that should be relatively straight forward and simple only to find this kind of disjointed nonsense.

And yes, I know this may have been a result of the internal designer decisions. But you know what? I have seen this in many compilers I have worked with over the years and it seems that in more than a few instances the internal designers are completely out of touch with reality.

What else would a developer want most from a PropertyInfo object than a property's value?

Thank you...

What I have tried:

I have done quite a bit of research on this issue with practically all of the found solutions not providing working results. The solution noted in my question has been the only working solution I have been able to find in this regard...
Posted
Updated 25-Jan-20 22:07pm
v4
Comments
Wendelius 26-Jan-20 4:29am    
Not sure why the question was downvoted so countered. I would believe that many others have the same question.

Your "Get property", in effect, creates a "generic" property accessor that can be used with any object that contains "that" property:--

That's why you supply an object (again) in the GetValue (or SetValue), because it doesn't have to be the same class that you used to create the "info" object in the first place.

Used in a loop, you can clone classes with the same interfaces; get property once, use it to get (value) from one, then to set the other.
 
Share this answer
 
To add to solution 1: The information you get using reflection, for example from GetProperties, GetMethods, etc. are related to the class definition, not to an instance of that class.

So when you want to invoke a method or get a value from a property, you need to specify the object instance which is going to be called. As pointed out this is extremely useful when you need to get values from multiple instances of the same class. Getting information using reflection is slow so you want to do that only once especially if you have hundreds or thousands of objects to get the data from.

Also thinking about the class. As said the information you get from reflection is type specific. On the other hand when you invoke a method the instance does not have to be the same class, it can be for example a derived class or simply implement the interface.

For example, if you have a base class Vehicle and few derived classes like Car, Train, Aeroplane. The base class could implement a property IsFree. Using reflection you can get property information for that property by querying it from the Vehicle class. However, when getting the actual value, the class you use can be any of the derived classes.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900