A personal repository of technical notes. - CSC

How to Create .NET Object From String

Problem
Need to generate an instance of a class dynamically from a string value.

Solution
Use Assembly.CreateInstance Method to create an instance of a type name.
  

Assembly currentAssembly = Assembly.GetExecutingAssembly();

MyClass myClassObjectFromString = null;

try

{

myClassObjectFromString = currentAssembly.CreateInstance("MyNamespace.MyClass") as MyClass;

}

catch

{

}

if (myClassObjectFromString != null)

{

//

}

References
"Assembly.GetExecutingAssembly Method (System.Reflection)." MSDN: Microsoft Development, MSDN Subscriptions, Resources, and More. 23 June 2009 http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getexecutingassembly(VS.80).aspx.

"Assembly.CreateInstance Method (String) (System.Reflection)." MSDN: Microsoft Development, MSDN Subscriptions, Resources, and More. 23 June 2009 http://msdn.microsoft.com/en-us/library/dex1ss7c(VS.80).aspx.

See Also
"How to Convert an ASP.NET Control to String |." CSC - Technical Notes. 23 June 2009 http://csc-technicalnotes.blogspot.com/2009/03/how-to-convert-control-to-html-string.html.

Unable to Disable ASP.NET Themes

Problem
Received the following error on web page that did not require themes:

Using themed css files requires a header control on the page. (e.g. <head runat="server" />).

Setting EnableTheming="false" did not work.

Solution
If themes are not required on page, disable themes with additional attributes:

<%@ Page EnableTheming="false" Theme="" StylesheetTheme="" %>

Note: It is undocumented that you must specify empty attributes Theme and StylesheetTheme.

References
"How to: Disable ASP.NET Themes." MSDN: Microsoft Development, MSDN Subscriptions, Resources, and More. 03 June 2009 http://msdn.microsoft.com/en-us/library/kx3kzht7(VS.80).aspx.