How to use generated assembly?

I have a dotnet dll name test.dll after using sgen I got test.XmlSerializers.dll

Now my types in the generated dll are suffixed with “Serializer” keywork i.e

If I have testclass I get testclassSerializer type in the generated dll. How can I use these generated types for deserialization



Answer this question

How to use generated assembly?

  • eroe

    Hi Rodrigo,

    Thank you very much for the response.

    // Create an instance of the XmlSerializer specifying type.
    class1Serializer serializer =
    new class1Serializer (typeof(class1));

    generates an error "No overload for method 'class1Serializer' takes '1' arguments"

    the constructor present in the generated assembly is not accepting the argument (typeof(class1))

    if I try class1Serializer serializer = new class1Serializer ();

    class1 c1 = (class1) serializer.Deserialize(reader);

    generates an error “unable to cast object of type class1 to type class1”

    I have around 150 types in my generated assembly I am trying to create a generic deserializer method which will deserialize the types passed to it. I am trying to do this to increase the performance by avoiding creating serializer object in the runtime like this

    XmlSerializer serializer = new XmlSerializer(typeof(class1));

    class1 c1 = (Class1)serializer.Deserialize(reader)

    for more than 150 classes. During runtime I will deserialize the necessary types.

    I got stuck while deserialization Will this approach work


  • mpco

    I want it for manual [design time serialization]. I have around 150 classes to be serialized. I created a dll of these types and used sgen to create another dll with serialized types [readers/writers]. Now I want to deserialize them.

    Example

    If give test.dll as input to sgen I will get test.XmlSerializers.dll I need to use this dll in my project and deserialize.

    I have class1 and after sgen I get class1Serializer in the generated assembly.

    How do I deserialize


  • cerny

    you want this for manual deserialization/serialization or for use it with web service proxies


  • MichaelLee

    I tried this:

    CiudadSerializer cs = new CiudadSerializer();

    Ciudad city2 = (Ciudad)cs.Deserialize(new FileStream(@"C:\Temp\city.xml", FileMode.Open));

    And it works fine for me..... are you sure this doesn't work in your code


  • Dave Citron

    I used sgen exe to generate the dll now i want to de- serialize using the types present in the generated assembly. Any suggestion in this regard is welcomed.Thanks


  • Enkht

    Well, sgen generates an optimized XmlSerializer derived class for each of your types.

    To use it, just add a reference in your project to test.XmlSerializers.dll and then use the class1Serializer in the same way you used XmlSerializer class:

    private void DeserializeObject(string filename)
    {
    Console.WriteLine("Reading with TextReader");

    // Create an instance of the XmlSerializer specifying type.
    class1Serializer serializer =
    new class1Serializer (typeof(OrderedItem));

    // Create a TextReader to read the file.
    FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
    TextReader reader = new StreamReader(fs);

    // Declare an object variable of the type to be deserialized.
    OrderedItem i;

    // Use the Deserialize method to restore the object's state.
    i = (OrderedItem) serializer.Deserialize(reader);

    // Write out the properties of the object.
    Console.Write(
    i.ItemName + "\t" +
    i.Description + "\t" +
    i.UnitPrice + "\t" +
    i.Quantity + "\t" +
    i.LineTotal);
    }

    Rgds

    Rodrigo


  • jkitagr

    hi

    can any one guide me on how to de-serialize sgen generated assembly

    thank you


  • Vyatsek

    hi

    It generates an error “unable to cast object of type class1 to type class1” - InvalidCastException.

    any idea how to over come this


  • How to use generated assembly?