Author Topic: C# Reflection - Changing Property Value at run time  (Read 926 times)


  • Administrator
  • Full Member
  • *****
  • Posts: 69
C# Reflection - Changing Property Value at run time
« on: November 01, 2011, 12:18:20 pm »
The following code would load the C# assembly at run time with System.Reflection.Assembly.LoadFile(filename) . Then it try to get the class name called "CMyTestClass" and sets the value as "Software&Finance" for the property named "MyPropertyName".

Code: (C#)
//Source Code Listing

Assembly assembly = Assembly.LoadFile("c:\\testserver.dll");
Type classType = assembly.GetType("CMyTestClass");
object classObjInstance = Activator.CreateInstance(classType);

// Note in this case, you do not need to have the assembly file loaded in the GAC. And also it does not bound to a specific version of the assembly. 

PropertyInfo info = classType.GetProperty("MyPropertyName");
object[] args = { "Software&Finance" };
info.SetValue(classObjInstance, args[0], null);

« Last Edit: January 11, 2012, 12:32:42 pm by kathir »


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1