Csharp/CSharp Tutorial/Assembly/Assembly Attributes

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Assembly attributes: AssemblyVersionAttribute, AssemblyTitleAttribute

<source lang="csharp">using System; using System.Reflection; using System.Windows.Forms; [assembly:AssemblyVersionAttribute("1.0.0.0")] [assembly:AssemblyTitleAttribute("Example")] class MainClass {

 public static void Main() 
 {
 }

}</source>

Dynamic Asm Builder

<source lang="csharp">using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.Reflection.Emit; using System.Threading;

 class Program
 {
   static void Main(string[] args)
   { 
     AppDomain curAppDomain = Thread.GetDomain();
     CreateMyAsm(curAppDomain);
     Assembly a = Assembly.Load("MyAssembly");
     Type hello = a.GetType("MyAssembly.HelloWorld");
     string msg = "asdf";
     object[] ctorArgs = new object[1];
     ctorArgs[0] = msg;
     object obj = Activator.CreateInstance(hello, ctorArgs); 
     MethodInfo mi = hello.GetMethod("SayHello");
     mi.Invoke(obj, null);
     mi = hello.GetMethod("GetMsg");
     Console.WriteLine(mi.Invoke(obj, null));
   } 
   public static void CreateMyAsm(AppDomain curAppDomain)
   {
     AssemblyName assemblyName = new AssemblyName();
     assemblyName.Name = "MyAssembly";
     assemblyName.Version = new Version("1.0.0.0");
     AssemblyBuilder assembly =curAppDomain.DefineDynamicAssembly(assemblyName,AssemblyBuilderAccess.Save);
     ModuleBuilder module = assembly.DefineDynamicModule("MyAssembly", "MyAssembly.dll");
     TypeBuilder helloWorldClass = module.DefineType("MyAssembly.HelloWorld",TypeAttributes.Public);
     FieldBuilder msgField = helloWorldClass.DefineField("theMessage", Type.GetType("System.String"),FieldAttributes.Private);
     Type[] constructorArgs = new Type[1];
     constructorArgs[0] = typeof(string);
     ConstructorBuilder constructor = helloWorldClass.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, constructorArgs);
     ILGenerator constructorIL = constructor.GetILGenerator();
     constructorIL.Emit(OpCodes.Ldarg_0);
     Type objectClass = typeof(object);
     ConstructorInfo superConstructor = objectClass.GetConstructor(new Type[0]);
     constructorIL.Emit(OpCodes.Call, superConstructor);
     constructorIL.Emit(OpCodes.Ldarg_0);
     constructorIL.Emit(OpCodes.Ldarg_1);
     constructorIL.Emit(OpCodes.Stfld, msgField);
     constructorIL.Emit(OpCodes.Ret);
     helloWorldClass.DefineDefaultConstructor(MethodAttributes.Public);
     MethodBuilder getMsgMethod = helloWorldClass.DefineMethod("GetMsg", MethodAttributes.Public, typeof(string), null);
     ILGenerator methodIL = getMsgMethod.GetILGenerator();
     methodIL.Emit(OpCodes.Ldarg_0);
     methodIL.Emit(OpCodes.Ldfld, msgField);
     methodIL.Emit(OpCodes.Ret);
     MethodBuilder sayHiMethod = helloWorldClass.DefineMethod("SayHello", MethodAttributes.Public, null, null);
     methodIL = sayHiMethod.GetILGenerator();
     methodIL.EmitWriteLine("Hello!");
     methodIL.Emit(OpCodes.Ret);
     helloWorldClass.CreateType();
     assembly.Save("MyAssembly.dll");
   }
 }</source>

Set assembly attributes

<source lang="csharp">using System; using System.Collections.Generic; using System.Text; using System.EnterpriseServices; [assembly: ApplicationName("Demo")] [assembly: Description("C#")] [assembly: ApplicationActivation(ActivationOption.Server)] [assembly: ApplicationAccessControl(false)]

public interface IGreeting {

   string Welcome(string name);

} [EventTrackingEnabled(true)] [Description("Simple Serviced Component Sample")] public class SimpleComponent : ServicedComponent, IGreeting {

   public SimpleComponent() {
   }
   public string Welcome(string name) {
       // simulate some processing time
       System.Threading.Thread.Sleep(1000);
       return "Hello, " + name;
   }

} class Program {

   static void Main(string[] args) {
       using (SimpleComponent obj = new SimpleComponent()) {
           for (int i = 0; i < 10; i++) {
               Console.WriteLine(obj.Welcome("Kathie"));
           }
       }
   }

}</source>