23rd August 2007, 04:44 pm
Creating a class from a string might be crucial for some very dynamic projects. It’s a single line of code that I wanted to share but I think it has a lot of power.
Simply get the executing assembly and call the GetType method. If your assembly is one of the linked assembly or even a dynamically loaded assembly, you might need to call GetReferencedAssemblies() method as well.
namespace reflectme
{
using System;
public class hello
{
public hello()
{
Console.WriteLine("hello");
Console.ReadLine();
}
static void Main(string[] args)
{
Type t = System.Reflection.Assembly.GetExecutingAssembly().GetType("reflectme.hello");
t.GetConstructor(System.Type.EmptyTypes).Invoke(null);
}
}
}
Bear in mind it will be extremely slow. Don’t do that
12th June 2006, 02:11 am
About Me
Hi, I’m Can Erten. It is pronounced as “jän”. I had my Bachelors and Masters in Computer Science in which I focused on parallel programming and functional programming.
Last year, I visited Microsoft Research as a research intern and worked on F#. Now, I’m working at Microsoft on Entertainment and Devices team working with different products. Prior to joining the company, I have worked in consultancy and software companies.
My interests are :
Common Language Runtime (CLR)
Object Oriented Programming (C#, Java)
Functional Programming (F#)
Parallel and Distributed Programming
Web Frameworks (ASP.NET /Ajax)
About this Blog
I will share my programming, web experiences, you will meet coding side of my life.. Most of my work, not all, will be for .NET Framework. Also I will look at some new technologies in the software industry. You will also see some projects done at my spare time.