private static string BeginReadGenericData(string type, TypeLookupModeEnum typeLookupMode, bool findNewestTypeVersion, bool ignoreCase, ref int index) { string typeName = string.Empty; string typeChar = string.Empty; while (index < type.Length) { typeChar = type.Substring(index, 1); if ("[".Equals(typeChar)) { index++; typeName = string.Format("{0}[{1}]", typeName, BeginReadInnerType(type, typeLookupMode, findNewestTypeVersion, ignoreCase, ref index).AssemblyQualifiedName); } else if ("]".Equals(typeChar)) { // egy generikus paraméter beolvasása megtörtént break; } else { typeName = typeName + typeChar; } index++; } return(typeName); }
/// <summary> /// Initializes a new instance of the <see cref="TypeKey" /> class. /// </summary> /// <param name="asmName">Name of the asm.</param> /// <param name="typeName">Name of the type.</param> /// <param name="typeLookupMode">The type lookup mode.</param> /// <param name="findNewestTypeVersion">if set to <c>true</c> [find newest type version].</param> internal TypeKey(AssemblyName asmName, string typeName, TypeLookupModeEnum typeLookupMode, bool findNewestTypeVersion) { this.AsmName = asmName; this.TypeName = typeName; this.TypeLookupMode = typeLookupMode; this.FindNewestTypeVersion = findNewestTypeVersion; }
private static Type GetTypeFromString(string assemblyFullQualifiedName, TypeLookupModeEnum typeLookupMode, bool findNewestTypeVersion, bool throwOnError, bool ignoreCase, ref int index) { Type result = null; int i = assemblyFullQualifiedName.IndexOf(", "); try { if (assemblyFullQualifiedName.Contains("`")) { // generikus típus result = BeginReadInnerType(assemblyFullQualifiedName, typeLookupMode, findNewestTypeVersion, ignoreCase, ref index); } else if (i > 0) { // normál assembly-vel string assembly = assemblyFullQualifiedName.Substring(i + 1).Trim(); AssemblyName asmName = new AssemblyName(assembly); string typeStrName = assemblyFullQualifiedName.Substring(0, i); result = GetTypeFromString(asmName, typeStrName, typeLookupMode, findNewestTypeVersion, ignoreCase); } else { // normál assembly nélkül result = GetTypeFromString(null, assemblyFullQualifiedName, typeLookupMode, findNewestTypeVersion, ignoreCase); } } catch (TypeLoadException ex) { if (LOGGER.IsDebugEnabled) { LOGGER.Debug(string.Format("TypeLoader, unable to load assembly: {0}", assemblyFullQualifiedName), ex); } if (throwOnError) { throw; } } catch (Exception ex) { if (LOGGER.IsDebugEnabled) { LOGGER.Debug(string.Format("TypeLoader, unable to load assembly: {0}", assemblyFullQualifiedName), ex); } if (throwOnError) { throw new TypeLoadException(string.Format("Type '{0}' not found.", assemblyFullQualifiedName), ex); } } if (result == null && throwOnError) { throw new TypeLoadException(string.Format("Type '{0}' not found.", assemblyFullQualifiedName)); } return(result); }
/// <summary> /// Gets the type from string. /// </summary> /// <param name="assemblyFullQualifiedName">Name of the assembly full qualified.</param> /// <param name="typeLookupMode">The type lookup mode.</param> /// <param name="findNewestTypeVersion">Find the newest version from a type is mandatory or not.</param> /// <param name="throwOnError">if set to <c>true</c> [throw on error].</param> /// <param name="ignoreCase">if set to <c>true</c> [ignore case].</param> /// <returns>The type if it was resolved, otherwise False.</returns> /// <example> /// <code> /// Type type = null; /// /// type = TypeHelper.GetTypeFromString("System.Int32", true); /// Assert.IsFalse(type == null); /// /// type = TypeHelper.GetTypeFromString("System.Collections.Generic.Dictionary`2[[System.Int32],[System.Int64]]", true); /// Assert.IsFalse(type == null); /// /// type = TypeHelper.GetTypeFromString("System.Collections.Generic.Dictionary`2[[System.Int32],[System.Int64]][,]", true); /// Assert.IsFalse(type == null); /// /// type = TypeHelper.GetTypeFromString(typeof(Dictionary<int, Dictionary<int, string>>).AssemblyQualifiedName); /// Assert.IsFalse(type == null); /// /// type = TypeHelper.GetTypeFromString(typeof(Dictionary<int, Dictionary<int, string>>[]).AssemblyQualifiedName); /// Assert.IsFalse(type == null); /// /// type = TypeHelper.GetTypeFromString(typeof(HashSet<int?>[][,]).AssemblyQualifiedName, true); /// Assert.IsFalse(type == null); /// /// type = TypeHelper.GetTypeFromString(typeof(HashSet<int?>[][,]).AssemblyQualifiedName, TypeLookupModeEnum.AllowAll, true, false, false); /// Assert.IsFalse(type == null); /// </code> /// </example> public static Type GetTypeFromString(string assemblyFullQualifiedName, TypeLookupModeEnum typeLookupMode, bool findNewestTypeVersion, bool throwOnError, bool ignoreCase) { if (string.IsNullOrEmpty(assemblyFullQualifiedName)) { ThrowHelper.ThrowArgumentNullException("assemblyFullQualifiedName"); } int startIndex = 0; return(GetTypeFromString(assemblyFullQualifiedName, typeLookupMode, findNewestTypeVersion, throwOnError, ignoreCase, ref startIndex)); }
/// <summary> /// Gets the type from string. /// </summary> /// <param name="assemblyFullQualifiedName">Name of the assembly full qualified.</param> /// <param name="typeLookupMode">The type lookup mode.</param> /// <returns>The type if it was resolved, otherwise False.</returns> /// <example> /// <code> /// Type type = null; /// /// type = TypeHelper.GetTypeFromString("System.Int32", true); /// Assert.IsFalse(type == null); /// /// type = TypeHelper.GetTypeFromString("System.Collections.Generic.Dictionary`2[[System.Int32],[System.Int64]]", true); /// Assert.IsFalse(type == null); /// /// type = TypeHelper.GetTypeFromString("System.Collections.Generic.Dictionary`2[[System.Int32],[System.Int64]][,]", true); /// Assert.IsFalse(type == null); /// /// type = TypeHelper.GetTypeFromString(typeof(Dictionary<int, Dictionary<int, string>>).AssemblyQualifiedName); /// Assert.IsFalse(type == null); /// /// type = TypeHelper.GetTypeFromString(typeof(Dictionary<int, Dictionary<int, string>>[]).AssemblyQualifiedName); /// Assert.IsFalse(type == null); /// /// type = TypeHelper.GetTypeFromString(typeof(HashSet<int?>[][,]).AssemblyQualifiedName, true); /// Assert.IsFalse(type == null); /// /// type = TypeHelper.GetTypeFromString(typeof(HashSet<int?>[][,]).AssemblyQualifiedName, TypeLookupModeEnum.AllowAll, true, false, false); /// Assert.IsFalse(type == null); /// </code> /// </example> public static Type GetTypeFromString(string assemblyFullQualifiedName, TypeLookupModeEnum typeLookupMode) { if (string.IsNullOrEmpty(assemblyFullQualifiedName)) { ThrowHelper.ThrowArgumentNullException("assemblyFullQualifiedName"); } int startIndex = 0; return(GetTypeFromString(assemblyFullQualifiedName, typeLookupMode, false, true, false, ref startIndex)); }
private static Type BeginReadInnerType(string type, TypeLookupModeEnum typeLookupMode, bool findNewestTypeVersion, bool ignoreCase, ref int index) { string typeName = string.Empty; string typeChar = string.Empty; string assembly = string.Empty; bool saveAssemblyInfo = false; while (index < type.Length) { typeChar = type.Substring(index, 1); if ("[".Equals(typeChar)) { index++; typeName = string.Format("{0}[{1}]", typeName, BeginReadGenericData(type, typeLookupMode, findNewestTypeVersion, ignoreCase, ref index)); saveAssemblyInfo = true; assembly = string.Empty; } else if ("]".Equals(typeChar)) { // egy generikus paraméter beolvasása megtörtént break; } else { typeName = typeName + typeChar; if (saveAssemblyInfo) { assembly = assembly + typeChar; } } index++; } Type result = null; if (string.IsNullOrEmpty(assembly)) { result = GetTypeFromString(null, typeName, typeLookupMode, findNewestTypeVersion, ignoreCase); } else { // generikus AssemblyName asmName = new AssemblyName(assembly.Substring(2, assembly.Length - 2)); string typeNameStr = typeName.Substring(0, typeName.Length - assembly.Length); result = GetTypeFromString(asmName, typeNameStr, typeLookupMode, findNewestTypeVersion, ignoreCase); } return(result); }
private static Type GetTypeFromString(AssemblyName asmName, string typeName, TypeLookupModeEnum typeLookupMode, bool findNewestTypeVersion, bool ignoreCase) { Type result = null; TypeKey typeKey = new TypeKey(asmName, typeName, typeLookupMode, findNewestTypeVersion); lock (mLockObject) { if (mTypeKeyVsType.ContainsKey(typeKey)) { result = mTypeKeyVsType[typeKey]; } else { if (asmName != null) { try { AppDomain.CurrentDomain.Load(asmName); } catch (Exception ex) { // ilyen akkor van, ha a DLL nem betölthető (hibás, nem található, security) if (LOGGER.IsDebugEnabled) { LOGGER.Debug(ex.Message, ex); } } } if (typeName.Contains("[")) { // ez egy array string typeNameWithoutArrayMarks = typeName.Substring(0, typeName.IndexOf("[")); Type elementType = GetTypeFromString(asmName, typeNameWithoutArrayMarks, typeLookupMode, findNewestTypeVersion, ignoreCase); result = Type.GetType(string.Format("{0}{1}, {2}", elementType.FullName, typeName.Substring(typeName.IndexOf("[")), elementType.Assembly.GetName().FullName)); } else { // pontos betöltés result = Type.GetType(typeName, false, ignoreCase); if (result == null && asmName != null) { // betöltés assembly-vel try { result = Type.GetType(string.Format("{0}, {1}", typeName, asmName.FullName), false, ignoreCase); } catch (Exception ex) { // ilyen akkor van, ha a DLL verziója más, mint ami az AppDomain-be be van töltve if (LOGGER.IsDebugEnabled) { LOGGER.Debug(ex.Message, ex); } } } // ha van assembly és nem engedélyezett az azonos verzió, akkor töröljük az eredményt if ((typeLookupMode & TypeLookupModeEnum.AllowExactVersions) == 0 && asmName != null && asmName.Version != null && result != null && result.Assembly.GetName().Version.Equals(asmName.Version)) { // azonos verzió nem engedélyezett result = null; } if (result == null || findNewestTypeVersion) { if (result != null && findNewestTypeVersion && result.IsArray) { // tömböt máshogy kezeljük Type elementType = result.GetElementType(); while (elementType.IsArray) { elementType = elementType.GetElementType(); } Type newestType = GetTypeFromString(elementType.AssemblyQualifiedName, typeLookupMode, true, false, ignoreCase); if (!result.Assembly.GetName().FullName.Equals(newestType.Assembly.GetName().FullName)) { // újabb típus string newTypeStr = string.Format("{0}, {1}", result.FullName, newestType.Assembly.GetName().FullName); result = GetTypeFromString(newTypeStr, TypeLookupModeEnum.AllowExactVersions, false, false, false); } } else { // manuális keresés az AppDomain assembly-k között // 1, ide akkor jutunk, ha a DLL nem létezik vagy a partial name nincs megadva a qualifyAssembly szekcióban // 2, engedélyeztük újabb verziók használatát foreach (Assembly a in new List <Assembly>(AppDomain.CurrentDomain.GetAssemblies())) { if (asmName == null || a.GetName().Name.Equals(asmName.Name)) { Type[] types = null; try { // találkoztam olyan dinamikus DLL-ekkel, amelyeknek a type-jait nem lehetett kiolvasni types = a.GetTypes(); } catch (Exception) { } if (types != null) { foreach (Type t in a.GetTypes()) { if (t.FullName.Equals(typeName)) { if (result == null) { // még nincs kiválasztott típus if (asmName != null && asmName.Version != null) { // van kiinduló verzió if ((typeLookupMode & TypeLookupModeEnum.AllowExactVersions) > 0 && asmName.Version.Equals(a.GetName().Version)) { // azonos verzió engedélyezett result = t; } else if ((typeLookupMode & TypeLookupModeEnum.AllowNewerVersions) > 0 && asmName.Version < a.GetName().Version) { // újabb verzió engedélyezett result = t; } else if ((typeLookupMode & TypeLookupModeEnum.AllowOlderVersions) > 0 && asmName.Version > a.GetName().Version) { // régebbi verzió engedélyezett result = t; } } else { // nincs kiinduló assembly verzió, nincs mihez viszonyítani a verzió, ezért az első alkalmas típus megfelelő result = t; } } else { // már van egy kiválasztott típusunk, attól keresünk megfelelőbbet (vagyis újabb verziót) if (asmName != null && asmName.Version != null) { // van kiinduló verzió if ((typeLookupMode & TypeLookupModeEnum.AllowExactVersions) > 0 && asmName.Version.Equals(a.GetName().Version)) { // azonos verzió engedélyezett result = t; } else if ((typeLookupMode & TypeLookupModeEnum.AllowNewerVersions) > 0 && asmName.Version < a.GetName().Version) { // újabb verzió engedélyezett result = t; } else if ((typeLookupMode & TypeLookupModeEnum.AllowOlderVersions) > 0 && asmName.Version > a.GetName().Version) { // régebbi verzió engedélyezett if (result.Assembly.GetName().Version < a.GetName().Version) { // ha már van exact vagy újabb verziónk, akkor attól régebbire már ne térjünk vissza result = t; } } } else { // nincs referencia verzió assembly, a legfrissebb type keresése if (result.Assembly.GetName().Version < a.GetName().Version) { // újabb verzió result = t; } } } break; } } } } } } } } if (result == null) { throw new TypeLoadException(string.Format("Type '{0}' not found.", typeName)); } string name = result.Assembly.GetName().Name; lock (mAssemblyNameVsTypeKey) { ListSpecialized <TypeKey> typeKeys = null; mTypeKeyVsType[typeKey] = result; if (mAssemblyNameVsTypeKey.ContainsKey(name)) { typeKeys = mAssemblyNameVsTypeKey[name]; } else { typeKeys = new ListSpecialized <TypeKey>(); mAssemblyNameVsTypeKey[name] = typeKeys; } typeKeys.Add(typeKey); } } } return(result); }
/// <summary> /// Initializes a new instance of the <see cref="BinarySerializerFormatter{T}"/> class. /// </summary> /// <param name="behavior">The behavior.</param> /// <param name="typeLookupMode">The type lookup mode.</param> /// <param name="findNewestTypeVersion">if set to <c>true</c> [find newest type version].</param> public BinarySerializerFormatter(BinarySerializerBehaviorEnum behavior, TypeLookupModeEnum typeLookupMode, bool findNewestTypeVersion) { this.mFormatter.SerializerBehavior = behavior; this.mFormatter.TypeLookupMode = typeLookupMode; this.mFormatter.FindNewestTypeVersion = findNewestTypeVersion; }
/// <summary> /// Initializes a new instance of the <see cref="DeserializationContext" /> class. /// </summary> /// <param name="selector">The selector.</param> /// <param name="context">The context.</param> /// <param name="serializerBehavior">The serializer behavior.</param> /// <param name="typeLookupMode">The type lookup mode.</param> /// <param name="findNewestTypeVersion">if set to <c>true</c> [find newest type version].</param> internal DeserializationContext(ISurrogateSelector selector, StreamingContext context, BinarySerializerBehaviorEnum serializerBehavior, TypeLookupModeEnum typeLookupMode, bool findNewestTypeVersion) { this.Selector = selector; this.Context = context; this.mSerializerBehavior = serializerBehavior; this.mTypeLookupMode = typeLookupMode; this.mFindNewestTypeVersion = findNewestTypeVersion; }