What do you think about this extension methods package?

CaptainC

New member
Joined
Mar 7, 2021
Messages
4
Programming Experience
10+
What do you think about this extension methods package?
It's well documented and adds a lot of functionality to strings specifically.
Any thoughts?
 
The documentation claims that Universal.Load<T>() and Universal.Save<T>() can load and save any object without needing to flag the object as Serializable. I was curious how it did this and looked. It uses the NetDataContractSerializer(), and it looks like the assertion is not true because the following code
C#:
using System;
using System.IO;
using System.Runtime.Serialization;

class Car
{
    public string Model { get; set; }
    public int Year { get; set; }
}

class Shell
{
    static void Main()
    {
        var carA = new Car() { Model = "Tesla", Year = 2018 };

        var memoryStream = new MemoryStream();

        var serializer = new NetDataContractSerializer();
        serializer.Serialize(memoryStream, carA);

        memoryStream.Seek(0, SeekOrigin.Begin);
        var carB = (Car)serializer.Deserialize(memoryStream);

    }
}
throws an exception:
C#:
System.Runtime.Serialization.InvalidDataContractException
  HResult=0x80131500
  Message=Type 'Car' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types.
  Source=System.Runtime.Serialization
  StackTrace:
   at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(String message, Type type)
   at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type)
   at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type)
   at System.Runtime.Serialization.NetDataContractSerializer.GetDataContract(RuntimeTypeHandle typeHandle, Type type, Hashtable& surrogateDataContracts)
   at System.Runtime.Serialization.NetDataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph)
   at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph)
   at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(Stream stream, Object graph)
   at Shell.Main()

Oh well. So much for any object.
 
Back
Top Bottom