Getting property value using reflection
-
I am trying to retrieve a property from the current executing assembly using the following but am getting a TargetException int MyInt = Convert.ToInt32(Type.GetType(Page.GetType().BaseType.FullName).GetProperty("MyIntProperty").GetValue(null,null)); Anybody know what I'm doing wrong? Thanks monkey say, monkey doo Uncle Monkey
-
I am trying to retrieve a property from the current executing assembly using the following but am getting a TargetException int MyInt = Convert.ToInt32(Type.GetType(Page.GetType().BaseType.FullName).GetProperty("MyIntProperty").GetValue(null,null)); Anybody know what I'm doing wrong? Thanks monkey say, monkey doo Uncle Monkey
Wow! You really should split your lines up into distinct pieces - it greatly helps during debugging your application. You're doing a few things that aren't necessary and would hamper performance, like
Type.GetType(Page.GetType().BaseType.FullName)
. Instead, you already have your Type using justPage.GetType().BaseType
. Also, when you callGetValue
you must pass the instance of the object (presumablyPage
) otherwise you would get an exception since you're trying to retrieve the value of anull
reference. Also, if you flatten your instance members, you really don't need to refer to the base Type to get the property where it's declared. UnlessBindingFlags.DeclaredOnly
is specified, all instance properties - both declared and inheritted - are reflected (not so with statics, unless you specifyBindingFlags.FlattenHierarchy
). In ASP.NET, which I assume you're using since you have aPage
object - though this could be anything - there is actually a large hierarchy of pages that extend from thePage
class, such as your .aspx file : your code-behind page class :Page
.Type t = Page.GetType();
PropertyInfo prop = t.GetProperty("MyIntProperty", BindingFlags.Instance |
BindingFlags.Public | BindingFlags.NonPublic);
int MyInt;
if (prop != null) MyInt = (int)prop.GetValue(Page, null);Microsoft MVP, Visual C# My Articles
-
Wow! You really should split your lines up into distinct pieces - it greatly helps during debugging your application. You're doing a few things that aren't necessary and would hamper performance, like
Type.GetType(Page.GetType().BaseType.FullName)
. Instead, you already have your Type using justPage.GetType().BaseType
. Also, when you callGetValue
you must pass the instance of the object (presumablyPage
) otherwise you would get an exception since you're trying to retrieve the value of anull
reference. Also, if you flatten your instance members, you really don't need to refer to the base Type to get the property where it's declared. UnlessBindingFlags.DeclaredOnly
is specified, all instance properties - both declared and inheritted - are reflected (not so with statics, unless you specifyBindingFlags.FlattenHierarchy
). In ASP.NET, which I assume you're using since you have aPage
object - though this could be anything - there is actually a large hierarchy of pages that extend from thePage
class, such as your .aspx file : your code-behind page class :Page
.Type t = Page.GetType();
PropertyInfo prop = t.GetProperty("MyIntProperty", BindingFlags.Instance |
BindingFlags.Public | BindingFlags.NonPublic);
int MyInt;
if (prop != null) MyInt = (int)prop.GetValue(Page, null);Microsoft MVP, Visual C# My Articles
Thanks, worked a treat monkey say, monkey doo Uncle Monkey