using System;
using System.Reflection;
public class MainClass
{
static void Main()
{
Assembly EntryAssembly;
EntryAssembly = Assembly.GetEntryAssembly();
foreach(AssemblyName Name in EntryAssembly.GetReferencedAssemblies())
Console.WriteLine("Name: {0}", Name.ToString());
}
}
Month: February 2011
Location of Assembly
using System;
using System.Reflection;
public class MainClass {
static void Main() {
Assembly EntryAssembly;
EntryAssembly = Assembly.GetEntryAssembly();
Console.WriteLine("Location: {0}", EntryAssembly.Location);
Console.WriteLine("Code Base: {0}", EntryAssembly.CodeBase);
Console.WriteLine("Escaped Code Base: {0}", EntryAssembly.EscapedCodeBase);
Console.WriteLine("Loaded from GAC: {0}", EntryAssembly.GlobalAssemblyCache);
}
}
AssemblyCulture AssemblyVersion
using System;
using System.Reflection;
[assembly:AssemblyCulture("")]
[assembly:AssemblyVersion("1.1.0.5")]
namespace MainClass
{
class MainClass
{
public static void Main()
{
Console.WriteLine("
Main method complete. Press Enter.");
}
}
}
Set AssemblyTitle, AssemblyDescription, AssemblyConfiguration, AssemblyCompany, AssemblyProduct
using System.Reflection;
using System.Windows.Forms;
[assembly: AssemblyTitle("title")]
[assembly: AssemblyDescription("code.")]
[assembly: AssemblyConfiguration("Retail")]
[assembly: AssemblyCompany("Inc.")]
[assembly: AssemblyProduct("C#")]
[assembly: AssemblyCopyright("Inc.")]
[assembly: AssemblyVersion("1.0.*")]
public class SimpleHelloWorld : Form
{
public static void Main()
{
Application.Run(new SimpleHelloWorld());
}
public SimpleHelloWorld()
{
Text = "Hello, WindowsForms!";
}
}
Dynamically invoking methods from classes in an assembly.
using System;
using System.Reflection;
interface IMyInterface {
void PrintAString(string s);
void PrintAnInteger(int i);
void PrintSomeNumbers(string desc, int i, double d);
int GetANumber(string s);
}
public class MyClass : IMyInterface {
public MyClass() {
}
public void PrintAString(string s) {
Console.WriteLine("PrintAString: {0}", s);
}
public void PrintAnInteger(int i) {
Console.WriteLine("PrintAnInteger: {0}", i);
}
public void PrintSomeNumbers(string desc, int i, double d) {
Console.WriteLine("PrintSomeNumbers String: {0}", desc);
Console.WriteLine("Integer: {0}", i);
Console.WriteLine("Double: {0}", d);
}
public int GetANumber(string s) {
Console.WriteLine("GetANumber: {0}", s);
return 99;
}
public int DoItAll(string s, int i, double d) {
IMyInterface mi = (IMyInterface)this;
mi.PrintSomeNumbers(s, i, d);
return mi.GetANumber(s);
}
}
public class MainClass {
public static void DoDynamicInvocation(string assembly) {
Assembly a = Assembly.LoadFrom(assembly);
foreach (Type t in a.GetTypes()) {
if (t.IsClass == false)
continue;
if (t.GetInterface("IMyInterface") == null)
continue;
Console.WriteLine("Creating instance of class {0}", t.FullName);
object obj = Activator.CreateInstance(t);
object[] args = { "Dynamic", 1, 99.6 };
object result;
result = t.InvokeMember("DoItAll",BindingFlags.Default|BindingFlags.InvokeMethod,null,obj,args);
Console.WriteLine("Result of dynamic call: {0}", result);
object[] args2 = { 12 };
t.InvokeMember("PrintAnInteger",BindingFlags.Default | BindingFlags.InvokeMethod,null,obj,args2);
}
}
public static void Main(string[] args) {
MyClass dmi = new MyClass();
dmi.PrintSomeNumbers("PrintMe", 1, 1.9);
int i = dmi.GetANumber("GiveMeOne");
Console.WriteLine("I = {0}", i);
DoDynamicInvocation(args[0]);
}
}
CurrentDomain, GetAssemblies
using System;
using System.Reflection;
using System.Globalization;
class MainClass
{
public static void Main()
{
ListAssemblies();
string name1 = "System.Data, Version=2.0.0.0," +"Culture=neutral, PublicKeyToken=b77a5c561934e089";
Assembly a1 = Assembly.Load(name1);
AssemblyName name2 = new AssemblyName();
name2.Name = "System.Xml";
name2.Version = new Version(2, 0, 0, 0);
name2.CultureInfo = new CultureInfo(""); //Neutral culture.
name2.SetPublicKeyToken(new byte[] {0xb7, 0x7a, 0x5c, 0x56, 0x19, 0x34, 0xe0, 0x89});
Assembly a2 = Assembly.Load(name2);
Assembly a3 = Assembly.Load("SomeAssembly");
Assembly a4 = Assembly.LoadFrom(@"c:sharedMySharedAssembly.dll");
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly a in assemblies)
{
Console.WriteLine(a.GetName());
}
}
}
System.Activator
using System;
using System.Reflection;
class AbstractFactory
{
public IReflect CreateObject(string classname)
{
IReflect objreflect;
try
{
System.Type oType = System.Type.GetType(classname,true);
objreflect = (IReflect)System.Activator.CreateInstance(oType);
}
catch(TypeLoadException e)
{
throw new InvalidOperationException("Type could " +
"not be created. Check innerException " +
"for details", e);
}
return objreflect;
}
};
public interface IReflect
{
string Name { get; }
};
class Reflect1 : IReflect
{
public string Name
{
get { return "Reflect 1"; }
}
};
class Reflect2 : IReflect{
public string Name {
get { return "Reflect 2"; }
}
}
class AbstractFactoryApp
{
static void Main(string[] args) {
callReflection(args[0]);
}
static void callReflection(string strClassName) {
IReflect objReflect;
AbstractFactory objFactory = new AbstractFactory();
try
{
objReflect = objFactory.CreateObject(strClassName);
Console.WriteLine("You constructed a {0} object",objReflect.Name);
} catch(Exception e) {
Console.WriteLine("ERROR: {0}
{1}", e.Message, (null == e.InnerException ? "" : e.InnerException.Message));
}
}
}

