CK.Core.SimpleTypeFinder.WeakResolver C# (CSharp) Method

WeakResolver() public static method

An implementation of Resolver that can be used to load types regardless of the version, culture, architecture and public key token (strongly-named assemblies) of the type names. (See WeakenAssemblyQualifiedName.)
The type name used is: "NamespaceOfTheType.TypeName, AssemblyName".
/// When is true, always throws a type load exception. /// The original error (not a ) is available in the . ///
public static WeakResolver ( string assemblyQualifiedName, bool throwOnError ) : Type
assemblyQualifiedName string
throwOnError bool
return System.Type
        public static Type WeakResolver( string assemblyQualifiedName, bool throwOnError )
        {
            Type done = StandardResolver( assemblyQualifiedName, false );
            if( ReferenceEquals( done, null ) )
            {
                string weakTypeName;
                if( !WeakenAssemblyQualifiedName( assemblyQualifiedName, out weakTypeName ) && throwOnError )
                {
                    throw new ArgumentException( String.Format( Impl.CoreResources.InvalidAssemblyQualifiedName, assemblyQualifiedName ), nameof( assemblyQualifiedName ) );
                }
                done = StandardResolver( weakTypeName, throwOnError );
            }
            return done;
        }

Usage Example

Esempio n. 1
0
        void DoDefaultInitialize()
        {
            const string _defType       = "System.Configuration.ConfigurationManager, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
            Type         configMananger = SimpleTypeFinder.WeakResolver(_defType, false);

            // Type op_equality is not portable: use ReferenceEquals.
            if (!ReferenceEquals(configMananger, null))
            {
                Type[]     stringParams   = new Type[] { typeof(string) };
                MethodInfo getAppSettings = configMananger.GetProperty("AppSettings", BindingFlags.Public | BindingFlags.Static).GetGetMethod();
                MethodInfo indexer        = getAppSettings.ReturnType.GetProperty("Item", typeof(string), stringParams).GetGetMethod();

                DynamicMethod getter = new DynamicMethod("CK-ReadConfigurationManagerAppSettings", typeof(string), stringParams, true);
                ILGenerator   g      = getter.GetILGenerator();
                g.EmitCall(OpCodes.Call, getAppSettings, null);
                g.Emit(OpCodes.Ldarg_0);
                g.EmitCall(OpCodes.Call, indexer, null);
                g.Emit(OpCodes.Ret);
                _initializedGetObject = _getObject = (Func <string, object>)getter.CreateDelegate(typeof(Func <string, object>));
                _initialized          = true;
            }
            else
            {
                throw new CKException(Impl.CoreResources.AppSettingsDefaultInitializationFailed);
            }
        }