// Constructor. public MemberInfoSerializationHolder(SerializationInfo info, StreamingContext context) { if (info == null) { throw new ArgumentNullException("info"); } memberType = (MemberTypes)(info.GetInt32("MemberType")); name = info.GetString("Name"); signature = info.GetString("Signature"); String assemblyName = info.GetString("AssemblyName"); String className = info.GetString("ClassName"); if (assemblyName == null || className == null) { throw new SerializationException (_("Serialize_StateMissing")); } Assembly assembly = FormatterServices.GetAssemblyByName (assemblyName); if (assembly == null) { throw new SerializationException (_("Serialize_StateMissing")); } containingType = FormatterServices.GetTypeFromAssembly (assembly, className); }
internal Type GetType(BinaryAssemblyInfo assemblyInfo, string name) { Type type = null; if (this.previousName != null && this.previousName.Length == name.Length && this.previousName.Equals(name) && this.previousAssemblyString != null && this.previousAssemblyString.Length == assemblyInfo.assemblyString.Length && this.previousAssemblyString.Equals(assemblyInfo.assemblyString)) { type = this.previousType; } else { type = this.Bind(assemblyInfo.assemblyString, name); if (type == null) { Assembly assembly = assemblyInfo.GetAssembly(); if (this.bSimpleAssembly) { ObjectReader.GetSimplyNamedTypeFromAssembly(assembly, name, ref type); } else { type = FormatterServices.GetTypeFromAssembly(assembly, name); } if (type != null) { ObjectReader.CheckTypeForwardedTo(assembly, type.Assembly, type); } } this.previousAssemblyString = assemblyInfo.assemblyString; this.previousName = name; this.previousType = type; } return(type); }
internal Type GetType(BinaryAssemblyInfo assemblyInfo, string name) { Type typeFromAssembly = null; if ((((this.previousName != null) && (this.previousName.Length == name.Length)) && (this.previousName.Equals(name) && (this.previousAssemblyString != null))) && ((this.previousAssemblyString.Length == assemblyInfo.assemblyString.Length) && this.previousAssemblyString.Equals(assemblyInfo.assemblyString))) { return(this.previousType); } typeFromAssembly = this.Bind(assemblyInfo.assemblyString, name); if (typeFromAssembly == null) { Assembly assm = assemblyInfo.GetAssembly(); if (this.bSimpleAssembly) { GetSimplyNamedTypeFromAssembly(assm, name, ref typeFromAssembly); } else { typeFromAssembly = FormatterServices.GetTypeFromAssembly(assm, name); } if (typeFromAssembly != null) { CheckTypeForwardedTo(assm, typeFromAssembly.Assembly, typeFromAssembly); } } this.previousAssemblyString = assemblyInfo.assemblyString; this.previousName = name; this.previousType = typeFromAssembly; return(typeFromAssembly); }
/* public Type this [Element element] * { * } */ public Type GetType(string xmlName, string xmlNamespace) { Type type = null; string localName = XmlConvert.DecodeName(xmlName); string namespaceURI = XmlConvert.DecodeName(xmlNamespace); string typeNamespace, assemblyName; #if FIXED SoapServices.DecodeXmlNamespaceForClrTypeNamespace( xmlNamespace, out typeNamespace, out assemblyName); #else typeNamespace = null; assemblyName = null; #endif string typeName = (typeNamespace == null || typeNamespace == String.Empty) ? localName : typeNamespace + Type.Delimiter + localName; if (assemblyName != null && assemblyName != string.Empty && _binder != null) { type = _binder.BindToType(assemblyName, typeName); } if (type == null) { string assemblyQualifiedName = (string)xmlNodeToTypeTable[GetKey(xmlName, xmlNamespace)]; if (assemblyQualifiedName != null) { type = Type.GetType(assemblyQualifiedName); } else { type = Type.GetType(typeName); if (type == null) { if (assemblyName == null || assemblyName == String.Empty) { throw new SerializationException( String.Format("Parse Error, no assembly associated with XML key {0} {1}", localName, namespaceURI)); } type = FormatterServices.GetTypeFromAssembly( Assembly.Load(assemblyName), typeName); } } if (type == null) { throw new SerializationException(); } } return(type); }
private Type LoadTypeFromAssembly(string assemblyName, string typeName) { if (string.IsNullOrEmpty(assemblyName) || string.IsNullOrEmpty(typeName)) { return(null); } var assembly = Assembly.Load(assemblyName); return(FormatterServices.GetTypeFromAssembly(assembly, typeName)); }
private static Type GetType(string assemblyString, string name) { Type type = _typeCache[name]; if (type == null) { type = FormatterServices.GetTypeFromAssembly(GetAssembly(assemblyString), name); _typeCache[name] = type; } return(type); }
private static void GetSimplyNamedTypeFromAssembly(Assembly assm, string typeName, ref Type type) { // Catching any exceptions that could be thrown from a failure on assembly load // This is necessary, for example, if there are generic parameters that are qualified with a version of the assembly that predates the one available try { type = FormatterServices.GetTypeFromAssembly(assm, typeName); } catch (TypeLoadException) { } catch (FileNotFoundException) { } catch (FileLoadException) { } catch (BadImageFormatException) { } }
static void LookIntoDeserialization() { //1 Assembly asm = Assembly.GetEntryAssembly(); Type t = FormatterServices.GetTypeFromAssembly(asm, "SClass"); //2 分配内存,拿到一个没有任何初始化的实例 object o = FormatterServices.GetUninitializedObject(t); //3 MemberInfo[] members = FormatterServices.GetSerializableMembers(t); //4 object[] values = null; //=序列化流中的数据 //5 object deserializeResult = FormatterServices.PopulateObjectMembers(o, members, values); }
private static void GetSimplyNamedTypeFromAssembly(Assembly assm, string typeName, ref Type?type) { // Catching any exceptions that could be thrown from a failure on assembly load // This is necessary, for example, if there are generic parameters that are qualified with a version of the assembly that predates the one available try { type = FormatterServices.GetTypeFromAssembly(assm, typeName); } catch (TypeLoadException) { } catch (FileNotFoundException) { } catch (FileLoadException) { } catch (BadImageFormatException) { } if (type == null) { type = Type.GetType(typeName, ResolveSimpleAssemblyName, new TopLevelAssemblyTypeResolver(assm).ResolveType, throwOnError: false); } }
internal Type FastBindToType(string assemblyName, string typeName) { Type type = null; TypeNAssembly entry = (TypeNAssembly)_typeCache.GetCachedValue(typeName); if (entry == null || entry.AssemblyName != assemblyName) { Assembly assm = null; try { assm = Assembly.Load(new AssemblyName(assemblyName)); } catch { } if (assm == null) { return(null); } if (_isSimpleAssembly) { GetSimplyNamedTypeFromAssembly(assm, typeName, ref type); } else { type = FormatterServices.GetTypeFromAssembly(assm, typeName); } if (type == null) { return(null); } // before adding it to cache, let us do the security check CheckTypeForwardedTo(assm, type.Assembly, type); entry = new TypeNAssembly(); entry.Type = type; entry.AssemblyName = assemblyName; _typeCache.SetCachedValue(entry); } return(entry.Type); }
internal Type?GetType(BinaryAssemblyInfo assemblyInfo, string name) { Type?objectType; if (((_previousName != null) && (_previousName.Length == name.Length) && (_previousName.Equals(name))) && ((_previousAssemblyString != null) && (_previousAssemblyString.Length == assemblyInfo._assemblyString.Length) && (_previousAssemblyString.Equals(assemblyInfo._assemblyString)))) { objectType = _previousType; } else { objectType = Bind(assemblyInfo._assemblyString, name); if (objectType == null) { Assembly sourceAssembly = assemblyInfo.GetAssembly(); if (_isSimpleAssembly) { GetSimplyNamedTypeFromAssembly(sourceAssembly, name, ref objectType); } else { objectType = FormatterServices.GetTypeFromAssembly(sourceAssembly, name); } // here let us do the security check if (objectType != null) { CheckTypeForwardedTo(sourceAssembly, objectType.Assembly, objectType); } } _previousAssemblyString = assemblyInfo._assemblyString; _previousName = name; _previousType = objectType; } return(objectType); }
private static void GetSimplyNamedTypeFromAssembly(Assembly assm, string typeName, ref Type type) { try { type = FormatterServices.GetTypeFromAssembly(assm, typeName); } catch (TypeLoadException) { } catch (FileNotFoundException) { } catch (FileLoadException) { } catch (BadImageFormatException) { } if (type == null) { type = Type.GetType(typeName, new Func <AssemblyName, Assembly>(ObjectReader.ResolveSimpleAssemblyName), new Func <Assembly, string, bool, Type>(new TopLevelAssemblyTypeResolver(assm).ResolveType), false); } }
private void PublicStatics() { MemberInfo[] ms = FormatterServices.GetSerializableMembers(typeof(A), new StreamingContext(StreamingContextStates.All)); foreach (MemberInfo m in ms) { if (!m.DeclaringType.Equals(typeof(A))) { throw new Exception("Err_358sfg! error"); } } A a = new A(); Object[] os = FormatterServices.GetObjectData(a, ms); if ((int)os[0] != 5) { throw new Exception("Err_293457tsgd! error"); } if ((String)os[1] != "Hello World") { throw new Exception("Err_394ts7g! error"); } Object o = FormatterServices.GetUninitializedObject(typeof(A)); Object o1 = FormatterServices.PopulateObjectMembers(o, ms, os); if (((A)o1).I != 5) { throw new Exception("Err_23947tsg! 2347sg"); } if (((A)o1).S != "Hello World") { throw new Exception("Err_w347sg! 2347sg"); } Type tp1 = FormatterServices.GetTypeFromAssembly(typeof(String).Assembly, "System.Int32"); if (!tp1.Equals(typeof(Int32))) { throw new Exception("Err_3497tsg! "); } Int32[] iS = { 1, 2, 3 }; Int32[] iD = new Int32[3]; Buffer.BlockCopy(iS, 0, iD, 0, 12); if (iD[0] != 1) { throw new Exception("Err_3w497gb! "); } if (iD[1] != 2) { throw new Exception("Err_3w497gb! "); } if (iD[2] != 3) { throw new Exception("Err_3w497gb! "); } Buffer.SetByte(iD, 4, 2); Double d1 = 0; Boolean parse = Double.TryParse("2.1", NumberStyles.Any, null, out d1); if (!parse) { throw new Exception("Err_29457sg! "); } if (d1 != 2.1) { throw new Exception("Err_23497tgz! "); } parse = TimeZone.IsDaylightSavingTime(DateTime.Now, new DaylightTime(new DateTime(2001, 1, 1), new DateTime(2001, 12, 1), new TimeSpan(TimeSpan.TicksPerHour))); }
void Call_FS_4() { Type tp = FormatterServices.GetTypeFromAssembly(typeof(string).Assembly, "System.Int32"); }
internal Type FastBindToType(string assemblyName, string typeName) { Type type = null; ObjectReader.TypeNAssembly typeNAssembly = (ObjectReader.TypeNAssembly) this.typeCache.GetCachedValue(typeName); if (typeNAssembly == null || typeNAssembly.assemblyName != assemblyName) { Assembly assembly = null; if (this.bSimpleAssembly) { try { ObjectReader.sfileIOPermission.Assert(); try { assembly = ObjectReader.ResolveSimpleAssemblyName(new AssemblyName(assemblyName)); } finally { CodeAccessPermission.RevertAssert(); } } catch (Exception ex) { } if (assembly == null) { return(null); } ObjectReader.GetSimplyNamedTypeFromAssembly(assembly, typeName, ref type); } else { try { ObjectReader.sfileIOPermission.Assert(); try { assembly = Assembly.Load(assemblyName); } finally { CodeAccessPermission.RevertAssert(); } } catch (Exception ex2) { } if (assembly == null) { return(null); } type = FormatterServices.GetTypeFromAssembly(assembly, typeName); } if (type == null) { return(null); } ObjectReader.CheckTypeForwardedTo(assembly, type.Assembly, type); typeNAssembly = new ObjectReader.TypeNAssembly(); typeNAssembly.type = type; typeNAssembly.assemblyName = assemblyName; this.typeCache.SetCachedValue(typeNAssembly); } return(typeNAssembly.type); }
internal Type FastBindToType(string assemblyName, string typeName) { Type typeFromAssembly = null; TypeNAssembly cachedValue = (TypeNAssembly)this.typeCache.GetCachedValue(typeName); if ((cachedValue == null) || (cachedValue.assemblyName != assemblyName)) { Assembly assm = null; if (this.bSimpleAssembly) { try { sfileIOPermission.Assert(); try { assm = ResolveSimpleAssemblyName(new AssemblyName(assemblyName)); } finally { CodeAccessPermission.RevertAssert(); } } catch (Exception) { } if (assm == null) { return(null); } GetSimplyNamedTypeFromAssembly(assm, typeName, ref typeFromAssembly); } else { try { sfileIOPermission.Assert(); try { assm = Assembly.Load(assemblyName); } finally { CodeAccessPermission.RevertAssert(); } } catch (Exception) { } if (assm == null) { return(null); } typeFromAssembly = FormatterServices.GetTypeFromAssembly(assm, typeName); } if (typeFromAssembly == null) { return(null); } CheckTypeForwardedTo(assm, typeFromAssembly.Assembly, typeFromAssembly); cachedValue = new TypeNAssembly { type = typeFromAssembly, assemblyName = assemblyName }; this.typeCache.SetCachedValue(cachedValue); } return(cachedValue.type); }
public bool runTest() { Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver : " + s_strDtTmVer); Console.WriteLine(); int iCountErrors = 0; int iCountTestcases = 0; String strLoc = "Loc_000oo"; Assembly asm1; Type tpValue; Type[] arrTpClassLib = null; try { do { strLoc = "Loc_435cd"; tpValue = typeof(String); asm1 = tpValue.Module.Assembly; iCountTestcases++; if (!tpValue.Equals(FormatterServices.GetTypeFromAssembly(asm1, "System.String"))) { iCountErrors++; Console.WriteLine("Err_743cd! Unexpected value reutned"); } strLoc = "Loc_02547cs"; arrTpClassLib = typeof(String).Module.GetTypes(); for (int i = 0; i < arrTpClassLib.Length; i++) { if (!arrTpClassLib[i].IsPublic || !arrTpClassLib[i].IsSerializable) { continue; } try { tpValue = FormatterServices.GetTypeFromAssembly(asm1, arrTpClassLib[i].FullName); iCountTestcases++; if (!tpValue.Equals(arrTpClassLib[i])) { iCountErrors++; Console.WriteLine("Err_743cd! Unexpected value reutned"); } }catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_846qm! Unexpected exception thrown for the type, " + arrTpClassLib[i].FullName + ", " + ex.ToString()); } } iCountTestcases++; if (FormatterServices.GetTypeFromAssembly(asm1, "No such string") != null) { iCountErrors++; Console.WriteLine("Err_743cd! Unexpected value reutned"); } tpValue = this.GetType(); asm1 = tpValue.Module.Assembly; iCountTestcases++; if (!tpValue.Equals(FormatterServices.GetTypeFromAssembly(asm1, "Co3956GetTypeFromAssembly_AsmStr"))) { iCountErrors++; Console.WriteLine("Err_743cd! Unexpected value reutned"); } iCountTestcases++; if (FormatterServices.GetTypeFromAssembly(asm1, "No such string") != null) { iCountErrors++; Console.WriteLine("Err_743cd! Unexpected value reutned"); } try { iCountTestcases++; FormatterServices.GetTypeFromAssembly(null, "Any String"); iCountErrors++; Console.WriteLine("Err_935vd! We were expecting this to throw!"); } catch (ArgumentNullException) {} catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_935fd! Unexpected exception thrown, " + ex.ToString()); } try { iCountTestcases++; FormatterServices.GetTypeFromAssembly(asm1, null); iCountErrors++; Console.WriteLine("Err_106vs! We were expecting this to throw!"); } catch (ArgumentNullException) {} catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_720cxa! Unexpected exception thrown, " + ex.ToString()); } } while (false); } catch (Exception exc_general) { ++iCountErrors; Console.WriteLine(s_strTFAbbrev + " : Error Err_8888yyy! strLoc==" + strLoc + ", exc_general==\n" + exc_general); } if (iCountErrors == 0) { Console.WriteLine("paSs. " + s_strTFPath + " " + s_strTFName + " ,iCountTestcases==" + iCountTestcases.ToString()); return(true); } else { Console.WriteLine("FAiL! " + s_strTFPath + " " + s_strTFName + " ,iCountErrors==" + iCountErrors.ToString() + " , BugNums?: " + s_strActiveBugNums); return(false); } }
internal Type?FastBindToType(string?assemblyName, string typeName) { Type?type = null; TypeNAssembly?entry = (TypeNAssembly?)_typeCache.GetCachedValue(typeName); if (entry == null || entry.AssemblyName != assemblyName) { // Check early to avoid throwing unnecessary exceptions if (assemblyName == null) { return(null); } Assembly? assm = null; AssemblyName?assmName; try { assmName = new AssemblyName(assemblyName); } catch { return(null); } if (_isSimpleAssembly) { assm = ResolveSimpleAssemblyName(assmName); } else { try { assm = Assembly.Load(assmName); } catch { } } if (assm == null) { return(null); } if (_isSimpleAssembly) { GetSimplyNamedTypeFromAssembly(assm, typeName, ref type); } else { type = FormatterServices.GetTypeFromAssembly(assm, typeName); } if (type == null) { return(null); } // before adding it to cache, let us do the security check CheckTypeForwardedTo(assm, type.Assembly, type); entry = new TypeNAssembly(); entry.Type = type; entry.AssemblyName = assemblyName; _typeCache.SetCachedValue(entry); } return(entry.Type); }
// Implement the IObjectReference interface. public Object GetRealObject(StreamingContext context) { Assembly assem; switch (type) { case UnityType.Empty: return(Empty.Value); case UnityType.DBNull: return(DBNull.Value); case UnityType.Missing: return(Missing.Value); case UnityType.ClrType: { if (data == null || data.Length == 0 || assembly == null) { throw new SerializationException (_("Serialize_StateMissing")); } if (assembly == String.Empty) { return(Type.GetType(data)); } assem = FormatterServices.GetAssemblyByName(assembly); if (assem == null) { throw new SerializationException (_("Serialize_StateMissing")); } Type clrType = FormatterServices.GetTypeFromAssembly(assem, data); if (clrType != null) { return(clrType); } throw new SerializationException (_("Serialize_StateMissing")); } // Not reached. case UnityType.Module: { assem = FormatterServices.GetAssemblyByName(assembly); if (assem == null) { throw new SerializationException (_("Serialize_StateMissing")); } try { Module module = assem.GetModule(data); if (module == null) { throw new SerializationException (_("Serialize_StateMissing")); } return(module); } catch (Exception) { throw new SerializationException (_("Serialize_StateMissing")); } } // Not reached. case UnityType.Assembly: { assem = FormatterServices.GetAssemblyByName(data); if (assem == null) { throw new SerializationException (_("Serialize_StateMissing")); } return(assem); } // Not reached. default: throw new ArgumentException (_("Arg_InvalidUnityObject")); } }
public void GetTypeFromAssembly_InvalidArguments_ThrowsException() { Assert.Throws <ArgumentNullException>("assem", () => FormatterServices.GetTypeFromAssembly(null, "name")); Assert.Null(FormatterServices.GetTypeFromAssembly(GetType().Assembly, Guid.NewGuid().ToString("N"))); // non-existing type doesn't throw }
private Type FastBindToType(string assemblyName, string typeName) { Type typeFromAssembly = null; TypeNAssembly cachedValue = (TypeNAssembly)this.typeCache.GetCachedValue(typeName); if ((cachedValue == null) || (cachedValue.AssemblyName != assemblyName)) { if (assemblyName == null) { return(null); } Assembly assm = null; AssemblyName name; try { name = new AssemblyName(assemblyName); } catch { return(null); } if (this.isSimpleAssembly) { assm = ResolveSimpleAssemblyName(name); } else { try { assm = Assembly.Load(name); } catch { } } if (assm == null) { return(null); } if (this.isSimpleAssembly) { GetSimplyNamedTypeFromAssembly(assm, typeName, ref typeFromAssembly); } else { typeFromAssembly = FormatterServices.GetTypeFromAssembly(assm, typeName); } if (typeFromAssembly == null) { return(null); } cachedValue = new TypeNAssembly { Type = typeFromAssembly, AssemblyName = assemblyName }; this.typeCache.SetCachedValue(typeName, cachedValue); } return(cachedValue.Type); }