动态获取dll映射实例

Imports System
Imports System.Reflection
Imports System.Security
Imports Microsoft.VisualBasic

Public Class MyClass1

    
Public Sub New()
    
End Sub
 'New

    
Public Sub New(ByVal i As Integer)
    
End Sub
 'New

    
Public Shared Sub Main()
        
Try
            
Dim myType As Type = GetType(MyClass1)
            
Dim types(0As Type
            types(
0= GetType(Int32)
            
' Get the constructor that takes an integer as a parameter.
            Dim constructorInfoObj As ConstructorInfo = myType.GetConstructor(types)
            
If Not (constructorInfoObj Is NothingThen
                Console.WriteLine(
"The constructor of MyClass that takes an integer as a parameter is: ")
                Console.WriteLine(constructorInfoObj.ToString())
            
Else
                Console.WriteLine(
"The constructor of MyClass that takes no " + "parameters is not available.")
            
End If

        
Catch e As Exception
            Console.WriteLine(
"Exception caught.")
            Console.WriteLine((
"Source: " + e.Source))
            Console.WriteLine((
"Message: " + e.Message))
        
End Try
    
End Sub
 'Main
End Class
 'MyClass1


public static MembershipProvider Instance() {

            
// Use the cache because the reflection used later is expensive
            Cache cache 
= HttpRuntime.Cache;
            Type type 
= null;
            
string cacheKey = null;

            
// Get the names of the providers
            MembershipConfiguration config 
= MembershipConfiguration.GetConfig();

            
// Read the configuration specific information
            
// for this provider
            Provider membershipProvider 
= (Provider) config.Providers[config.DefaultProvider];

            
// In the cache?
            cacheKey 
= "Membership::" + config.DefaultProvider;
            
if ( cache[cacheKey] == null ) {

                
// The assembly should be in \bin or GAC, so we simply need
                
// to get an instance of the type
                
try {

                    type 
= Type.GetType( membershipProvider.Type );

                    
// Insert the type into the cache
                    Type[] paramTypes 
= new Type[1];
                    paramTypes[
0= typeof(string);
                    cache.Insert( cacheKey, type.GetConstructor(paramTypes) );

                } 
catch (Exception e) {
                    
throw new Exception("Unable to load provider", e);
                }

            }

两个反映射代码。
posted on 2006-03-21 15:56  Sunny  阅读(145)  评论(0)    收藏  举报