SoFunction
Updated on 2025-03-07

C# reflection in actual application example code


/// <summary> 
/// Complete the proxy to obtain remote business logic objects from the client.
    /// </summary> 
    public static class FacadeService 
    { 
static IDictionary<string, Type> serviceClassCatalog;//Define a key-value pair interface object
        static FacadeService() 
        { 
            serviceClassCatalog = new Dictionary<string, Type>(); 
Assembly assembly = (new AssemblyName(["BizAsembly"]));//Start loading assembly object
Type[] types = ();//Get the type collection of all objects in the assembly
            Type baseType = typeof(MarshalByRefObject); 
            foreach (Type type in types) 
            { 
                if ((type)) 
                { 
                    Type[] interfaces = (); 
//The interface type finally derived from the interface type, that is, the highest-level interface is registered here.
                    if ( > 0) 
                    { 
                        (interfaces[0].FullName, type); 
                    } 
                } 
            } 
        } 

        /// <summary> 
//// Return the type object instance that implements the interface according to the incoming business logic class.
        /// </summary> 
/// <typeparam name="IFacade">Specific business logic interface type</typeparam>
/// <returns>Remote proxy for type object instances that implement this interface</returns>
        public static IFacade GetFacade<IFacade>() 
        { 
            string typeName = typeof(IFacade).FullName; 
            if ((typeName)) 
            { 
                object realProxy = (serviceClassCatalog[typeName]); 
                return (IFacade)realProxy; 
            } 
            else 
            { 
throw new Exception("Not included the service type defined by the interface.");
            } 
        } 
    }