Quantcast
Channel: PropertyInfo confusions
Viewing all articles
Browse latest Browse all 3

PropertyInfo confusions

$
0
0

Greetings,

I currently have a class that takes C# source files and compiles them during runtime using the CSharpCodeProvider. Using the following code as my example, is there a way for me to access the cars Wheel.Name property through the Car during runtime?

 

namespace CarExample
{publicclass Car
 {publicstring Name { get; set; }public Wheel VehicleWheel { get; set; }public Car()
  {
   VehicleWheel = new Wheel();
   VehicleWheel.Name = "MB Design";
  }
 }
}namespace CarExample
{publicclass Wheel
 {publicstring Name { get; set; }
 }
}

 

The above code is my example C# source files that are being compile during runtime. Next is the code that I'm trying to use to access the Name of the wheel through the car.

 

_ScriptAssembly = Assembly.LoadFile(InstallPath + "Scripts.dll");object Instance = Activator.CreateInstance(type);

PropertyInfo propertyInfo = Instance.GetType().GetProperty("Wheel.Name");return propertyInfo.GetValue(Instance, null);

 

However when I run the above code I receive a "Object reference not set to an instance of an object." error. The source files do instance the Wheel inside the Cars constructor, so the source file is not the issue. I'm assuming there is something else going on here that I'm not aware of. The Instance field contains the Car Type, it's not null so it's not an issue with the Type not being instanced. I'm pretty much at a loss with this.

If any of you could offer any suggestions I would greatly appreciate it!

-Johnathon

 

Edit:
PropertyInfo is null after I call GetProperty("Wheel.Name"). I'm assuming the reason is due to it not being able to access a nested property like how I'm trying to? The type argument passed to CreateInstance(type) is the Type found while looping through _ScriptAssembly.GetTypes(). I've varified the field to make sure that it's not null and it isn't. Like I mentioned previously, the Instance Field is not null, the Type is being instanced and returned correctly.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images