free geoip System.Reflection BaseType Woes - Jayson's Blog - jaysonKnight.com
jaysonKnight.com
Welcome to my corner of the internet

System.Reflection BaseType Woes

What does the following (vastly simplified) code display?

 
using System; 
namespace ReflectedProperty
{
 public class Person
 {
  public Person() { }
 }
} 
using System; 
namespace ReflectedProperty
{
 public class Employee : Person
 {
  public Employee() { }
 }
} 
using System; 
namespace ReflectedProperty
{
 public class Activity
 {
  public Activity() { } 
  private Employee[] _employee;
  public Employee[] Employee
  {
   get { return this._employee; }
   set { this._employee = value; }
  }
 }
} 
using System;
using System.Reflection;
using System.Windows.Forms; 
namespace ReflectedProperty
{
 class Class1
 {
  [STAThread]
  static void Main(string[] args)
  {
   Activity activity = new Activity(); 
   foreach (PropertyInfo info in activity.GetType().GetProperties())
   {
    MessageBox.Show(info.PropertyType.BaseType.Name);
   }
  }
 }
} 

Not at all what I’d expect, though I guess it (kind of) makes sense.  Anyone have an answer how to code around this one?


Posted Aug 07 2005, 11:23 AM by Jayson Knight

5 Comments

haacked@gmail.com (Haacked) wrote Re: System.Reflection BaseType Woes
on 08-08-2005 4:00 AM
If I remember correctly, all arrays are implicitely derived from System.Array. I remember that from the Richter book, because the implication is that all arrays are reference types. Though an array may contain value types, the array itself is a reference type and stored on the heap.
haacked wrote re: System.Reflection BaseType Woes
on 08-08-2005 4:00 AM
So I imagine your program would display "System.Array" or "Array".
Jayson Knight wrote re: System.Reflection BaseType Woes
on 08-08-2005 4:13 AM
That is indeed correct, and while it's not what I expected I fully understand why. So the problem is how can I access the base type of a type declared as an array? As much as I love reflection, it really makes my brain hurt sometimes :-).
Jayson Knight wrote re: System.Reflection BaseType Woes
on 08-08-2005 4:19 AM
Doh! I got it...the correct code is: MessageBox.Show(info.PropertyType.GetElementType().BaseType.Name); Per intellisense on GetElementType(): "The Type of the object encompassed or referred to by the current array, pointer or reference type." Sweet.
protected virtual void jaysonBlog { wrote Best of JaysonKnight.com -- Happy New Year!
on 12-30-2005 1:38 AM
One thing that really bugs me about blogs is that it’s still really damn hard to find specific...

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Copyright © :: JaysonKnight.com
External Content © :: Respective Authors

Terms of Service/Privacy Policy