Пример #1
0
        internal bool IsMatch( TypeName other )
        {
            var comparer = StringComparer.Ordinal;

            // if this type name doesn't have a namespace, then we can only match on name.  if we do have a namespace, then match on full name
            if ( ( !HasNamespace && comparer.Equals( Name, other.Name ) ) || comparer.Equals( FullName, other.FullName ) )
            {
                // if either type name doesn't have an assembly name, then treat them equally because they can't be compared
                if ( !IsAssemblyQualified || !other.IsAssemblyQualified || comparer.Equals( AssemblyName, other.AssemblyName ) )
                    return true;
            }

            return false;
        }
Пример #2
0
        private Type GetTypeByName( string typeName, bool throwOnError, Exception ex )
        {
            Contract.Requires( !string.IsNullOrEmpty( typeName ) );
            Contract.Requires( ex != null );

            EnsureAssemblyEntries();

            var sourceType = new TypeName( typeName );
            var types = ( from entry in entries
                          from type in entry.Assembly.ExportedTypes
                          let targetType = new TypeName( type )
                          where sourceType.IsMatch( targetType )
                          select type ).ToArray();

            switch ( types.Length )
            {
                case 0:
                    {
                        if ( throwOnError )
                            throw ex;

                        return null;
                    }
                case 1:
                    {
                        return types[0];
                    }
                default:
                    {
                        if ( throwOnError )
                            throw new AmbiguousMatchException( ExceptionMessage.AmbiguousTypeName.FormatDefault( typeName ) );

                        return null;
                    }
            }
        }