advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Download the code for this article
Wow! Writing client script with code is not uncommon, but now you can write compiled code that runs on the serverand that interacts with the same code that creates it. Is that cool, or what? Let us know in the csharp.general discussion group.
Partners & Affiliates
advertisement
advertisement
advertisement
Average Rating: 3.7/5 | Rate this item | 3 users have rated this item.
Generate and Execute Dynamic Script With .NET (cont'd)
The System.Reflection.Assembly class is one of the most important classes for implementing reflection-based techniques in your applications, because it exposes several methods for loading assemblies, which let you load a new assembly at runtime. After loading an assembly, you can use an instance of the Assembly object to query the assembly manifest data, determine available types for reference, and view metadata included with that block of application code.
advertisement


The Assembly.CreateInstance method supports dynamic invocation. You can use it to create a type reference based on the assembly. Because assemblies can contain numerous types, the Assembly class needs to support a method that returns a System.Type object for a specified class. It does that through the GetType method as shown the following code excerpt:

      // C# code
      object oClass;
       Type tType;
      string strFileName;
      string strClass;
   
      Assembly asm = Assembly.LoadFrom(strFileName);
       oClass = asm.CreateInstance(strClass);
      tType = asm.GetType(strClass);
 
The preceding code fragment shows how to use an Assembly object to load an assembly using the overloaded LoadFrom method, create an instance of a type reference using the assembly, and then create a System.Type object that represents the type reference by using the GetType method. Finally, you can use the Type object to invoke class members dynamically, using the overloaded InvokeMember method.

      // C# code
      object[] oArguments = new object[0];
   
       object oReturn = tType.InvokeMember("YourMethod",
         BindingFlags.Default | 
         BindingFlags.InvokeMethod,
         null, oClass, oArguments);
 
The preceding fragment demonstrates invoking the YourMethod member of the assembly using the Type instance obtained from the previous example. Note that the array oArguments must contain any input parameters required by the method; in this example oArguments is an array with 0 elements. By using the Assembly object and the Type object as shown in the examples you can load an assembly at runtime, create a reference to one of the assembly types, and then dynamically invoke one of the type methods.

Previous Page: Introduction Next Page: JScript.NET Basics


Page 1: IntroductionPage 3: JScript.NET Basics
Page 2: Dynamic Invocation with ReflectionPage 4: Dynamic Script Generation and Execution
Please rate this item (5=best)
 1  2  3  4  5
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About