How to get value from PropertyInfo
-
Is it possible to get the actual property value from a PropertyInfo object? I've tried the GetValue method with various parameters, but nothing works. I just get exceptions
-
Is it possible to get the actual property value from a PropertyInfo object? I've tried the GetValue method with various parameters, but nothing works. I just get exceptions
Etienne_123 wrote:
Is it possible to get the actual property value from a PropertyInfo object?
Of course it is.
Etienne_123 wrote:
I've tried the GetValue method with various parameters, but nothing works. I just get exceptions
You've posted here often enough to know that you should show both your code and the exception you get. If you'd care to post your code, we can look at it - but I suspect I know what part of your problem is here; I suspect you aren't passing the instance in to retrieve the value from.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Etienne_123 wrote:
Is it possible to get the actual property value from a PropertyInfo object?
Of course it is.
Etienne_123 wrote:
I've tried the GetValue method with various parameters, but nothing works. I just get exceptions
You've posted here often enough to know that you should show both your code and the exception you get. If you'd care to post your code, we can look at it - but I suspect I know what part of your problem is here; I suspect you aren't passing the instance in to retrieve the value from.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
This is a sample of a similar method I`m trying to use:
private string GetValueFromProperty(PropertyInfo pInfo)
{
return pInfo.GetValue(this, null).ToString();
//return pInfo.GetValue(null, null).ToString();
//return pInfo.GetValue(pInfo, null).ToString();
//return pInfo.GetValue(null, new object[] {}).ToString();
}The lines that are commented out are other options I've tried. All of the above gives me the following error:
"Object does not match target type"
-
This is a sample of a similar method I`m trying to use:
private string GetValueFromProperty(PropertyInfo pInfo)
{
return pInfo.GetValue(this, null).ToString();
//return pInfo.GetValue(null, null).ToString();
//return pInfo.GetValue(pInfo, null).ToString();
//return pInfo.GetValue(null, new object[] {}).ToString();
}The lines that are commented out are other options I've tried. All of the above gives me the following error:
"Object does not match target type"
But how are you creating your pInfo?
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
But how are you creating your pInfo?
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
List<PropertyInfo> properties = new List<PropertyInfo>();
properties.AddRange(someObject.GetType().GetProperties());
var pInfo = properties.Find(prop => prop.Name.Equals("someName"));This allows me to retrieve the appropriate PropertyInfo object
-
List<PropertyInfo> properties = new List<PropertyInfo>();
properties.AddRange(someObject.GetType().GetProperties());
var pInfo = properties.Find(prop => prop.Name.Equals("someName"));This allows me to retrieve the appropriate PropertyInfo object
And you want to retrieve the values from someObject, so use that in your GetValue call. For instance:
pInfo.GetValue(someObject, null);
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Etienne_123 wrote:
Is it possible to get the actual property value from a PropertyInfo object?
Of course it is.
Etienne_123 wrote:
I've tried the GetValue method with various parameters, but nothing works. I just get exceptions
You've posted here often enough to know that you should show both your code and the exception you get. If you'd care to post your code, we can look at it - but I suspect I know what part of your problem is here; I suspect you aren't passing the instance in to retrieve the value from.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
5 for correctly guessing the source of the problem before code was posted. The questioner was indeed not passing the object that he wanted to get the property value from.
Thanks. The psychic filter didn't let me down today ;)
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
And you want to retrieve the values from someObject, so use that in your GetValue call. For instance:
pInfo.GetValue(someObject, null);
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
Thanks that's the one thing I didn't try, and it worked :)
-
Thanks that's the one thing I didn't try, and it worked :)
No problem. It's the commonest problem with it, which was how I guessed at the top.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility