xml - Can objects created with .net reflection be serialized? -


Is it possible to serialize objects created through reflection? I get an error "Type 'testNameSpace.ScreenClass' object unable to type cast 'testNameSpace.ScreenClass'" when you try to sequence the display that was created through the object mirror. Error does not mean because the type is identical, so an artist should be successful.

Edit: This is the code that was not properly rendered in my original post

 Get the assembly from assembly file assembly. Assembly.LoadFile (@ "C: \ TestAssembly.exe"); Type type assembly type type = webAssembly.GetType ("testNameSpace.ScreenClass"); // Get Constructor for Type (Constructor does not take into parameters) Constructor Info CTRO = ScreenType Get Constructor (New Type [0]); // Create an example of type object screen = ctor.Invoke (new object [] {}); // Type "ID" property Get PropertyInfo screenId = screenType.GetProperty ("id"); // Populate the "ID" property of the Instance ScreenID Asset Value (screen, "1", blank); Console.light line ("value of id property:" + (string) screenID.gate value (screen, empty)); // Try sorting the example of objects created through reflection {FileStream fs = new FileStream ("serialized.xml", FileMode.Create); XmlSerializer serializer = New XmlSerializer (typef (testNameSpace.ScreenClass)); Serializer.Serialize (FS, Screen); // error is here fs.Close (); } Catch (Exception ex) {Console.WriteLine ( "Message: {0} \ nInnerException: {1} \ nStack trace: \ n {2}", ex.Message, ex.InnerException.Message, ex.StackTrace) ; } // Now create an example of the object without using reflection and test serializeNameSpace.ScreenClass screen2 = new testNameSpace.ScreenClass (); Screen2.id = "2"; Filestream fs = new filestream ("serialized xml", fliemmade.creative); XmlSerializer serializer = New XmlSerializer (typef (testNameSpace.ScreenClass)); Serializer.Serialize (FS, Screen 2); Fs.Close (); Console.light line ("\ ncirialized2.xml's content: \ n" + file.ReadAstest ("serialized 2.xml");  

Output to the console

  Message: There was an error creating the XML document. InnerException: Type 'testNameSpace.ScreenClass' to type 'testNameSpace.ScreenClass' unable to extract the object. Stack trace: System.Xml.Serialization.XmlSerializer.Serialize on SystemkXmlkSerializationkXmlSerializerkSerialize (XmlWriter XmlWriter object o, XmlSerializerNamespaces namespace, string encodingStyle, String id) (Stream stream, object o, XmlSerializerNamespaces namespace) on System.Xml.Serialization.XmlSerializer.Serialize (stream stream, object o) Program.GetOneProperty () in C: \ code \ Serialize.cs: serialized2.xml line 96 materials :? & Lt; XML version = "1.0"? & Gt; & Lt; Applictionskrinsskrin Ksmlns: Kssi = "Actiteepi://wwwkv3korg/200l/shmlskema-instans" Ksmlns: CSD = "Actiteepi://wwwkv3korg/200l/shmlskema" id = "2" / & gt;  

Vidhansbaklodfail () method is your problem, wrong it loads the assembly It is a bit difficult to load context references, Susan Cook talks about it in her blog when the serializer needs type, then she will use the Assembly.LoadFrom () and then load the assembly. Now it searches for a mismatch, types are never the same with different assemblies.

The loadfile () is only useful in very limited scenarios, as documented in the Remarks section of the MSDN Library article. You should use the assembly.LoadFrom () or Load () instead. Prefers the latter as it eliminates the possibility that an assembly of the same display name will be loaded with different files. That causes trouble because the assembly is not in the way of checking the program (it will not be if it is stored in c: \ ) to make sure the AppDomain.AssemblyResolve event handler is implemented Found the correct file is


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -