ISerializable-derived class, via FormatterServices.GetSerializableMembers() in c# - c#

A derived class that implements ISerializable, whose based class does NOT, can de/serialise both members of based class and itself. In the derived class, FormatterServices.GetSerializableMembers() is used to get the based class's members, it should return both field and property based on MSDN. However, in the code below, it only return field. Any idea?
MSDN
internal static class ISerializableVersioning {
public static void Go() {
using (var stream = new MemoryStream()) {
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, new Derived());
stream.Position = 0;
Derived d = (Derived)formatter.Deserialize(stream);
Console.WriteLine(d);
}
}
[Serializable]
private class Base {
protected String m_name = "base";
protected String Name { get { return m_name; } set { m_name = value; } }
public Base() { /* Make the type instantiable*/ }
}
[Serializable]
private class Derived : Base, ISerializable {
new private String m_name = "derived";
public Derived() { /* Make the type instantiable*/ }
// If this constructor didn't exist, we'd get a SerializationException
// This constructor should be protected if this class were not sealed
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
private Derived(SerializationInfo info, StreamingContext context) {
// Get the set of serializable members for our class and base classes
Type baseType = this.GetType().BaseType;
MemberInfo[] mi = FormatterServices.GetSerializableMembers(baseType, context);
// Deserialize the base class's fields from the info object
for (Int32 i = 0; i < mi.Length; i++) {
// Get the field and set it to the deserialized value
FieldInfo fi = (FieldInfo)mi[i];
fi.SetValue(this, info.GetValue(baseType.FullName + "+" + fi.Name, fi.FieldType));
}
// Deserialize the values that were serialized for this class
m_name = info.GetString("Name");
}
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
public virtual void GetObjectData(SerializationInfo info, StreamingContext context) {
// Serialize the desired values for this class
info.AddValue("Name", m_name);
// Get the set of serializable members for our class and base classes
Type baseType = this.GetType().BaseType;
//**Should GetSerializableMembers return both the field and property? But it only return field here**
MemberInfo[] mi = FormatterServices.GetSerializableMembers(baseType, context);
// Serialize the base class's fields to the info object
for (Int32 i = 0; i < mi.Length; i++) {
// Prefix the field name with the fullname of the base type
object value = ((FieldInfo) mi[i]).GetValue(this);
info.AddValue(baseType.FullName + "+" + mi[i].Name, value);
}
}
public override String ToString() {
return String.Format("Base Name={0}, Derived Name={1}", base.Name, m_name);
}
}
}

GetSerializableMembers returns a MemberInfo array; you are casting them all to FieldInfo even though they could be EventInfo, MethodBase, or PropertyInfo objects.

Related

C# How To Initialize a generic Class with a type variable

disclaimer I'm a newbie in understanding Reflection.
abstract class BaseClass<T>
{
public abstract T Value { get; }
public virtual bool CheckValue(string input)
{
return true;
}
}
class NotBaseClassA : BaseClass<string>
{
public override string Value { get => "Yes";}
public override bool CheckValue(string input)
{
return 1 == 2;
}
}
class NotBaseClassB : BaseClass<int>
{
public override int Value { get => 1; }
}
class ManyBaseClasses
{
public NotBaseClassB notBaseClassB;
public NotBaseClassA notBaseClassA;
}
class Programm
{
public void Main()
{
foreach (PropertyInfo pi in typeof(ManyBaseClasses).GetProperties())
{
string input = Console.ReadLine();
//Get the generic type of the propertyInfo
//BaseClass<type> bt = new BaseClass<type>();
//Instantiate BaseClass not as a var So I can do
//BaseClass.CheckValue(input)
}
}
}
I'm just trying to do like the title says. So i saw this answer but the answer returns a 'var' but I cant call my CheckValue() function from a type var. (Or i dont think I can?). What i Need is to Instantiate my BaseClass<> with the correct type from a type variable and not as a var, as a proper BaseClass obj so i can then call my functions.
Edit 1 : i've already managed to get the generic type in the variable by doing something like that
public static System.Type GetBaseClassType(this System.Type type)
{
System.Type[] types = new System.Type[]{ };
while (type != null && type != typeof(object) || types.Length == 0)
{
types = type.GetGenericArguments();
if (types.Length > 0)
{
return types[0];
}
type = type.BaseType;
}
return null;
}
The base class is not relevant in this case, as it's abstract, so you actually want to instantiate the derived class.
All you need to do to create it is
Activator.CreateInstance(pi.PropertyType)
Then you will need to use reflection on that result to call CheckValue, because there is no common base type or interface.
It might be easier to extract the non-generic code into a BaseBaseClass which is not generic, which means you don't need reflection for the second step.
abstract class BaseBaseClass
{
public virtual bool CheckValue(string input)
{
return true;
}
}
abstract class BaseClass<T> : BaseBaseClass
{
public abstract T Value { get; }
}
Then you can just do
((BaseBaseClass) Activator.CreateInstance(pi.PropertyType)).CheckValue(someInput)
I've done that before, but it was a long time ago. You have to create instance via reflection and call the method via reflection.
foreach (PropertyInfo pi in typeof(ManyBaseClasses).GetProperties())
{
string input = Console.ReadLine();
//Get the generic type of the propertyInfo
var propType = pi.Type;
Type[] typeArgs = { propType };
var genType = d1.MakeGenericType(typeArgs);
//BaseClass<type> bt = new BaseClass<type>();
object bt = Activator.CreateInstance(genType);
//Instantiate BaseClass not as a var So I can do
//BaseClass.CheckValue(input)
MethodInfo method = typeof(bt).GetMethod("CheckValue"));
method.Invoke(bt, new[] { input });
}

C# Reflection: Static Property NullPointer

i wrote some code to get all classes implementing an Interface.
private static List<ClassNameController> getClassesByInheritInterface(Type interfaceName)
{
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => interfaceName.IsAssignableFrom(p) && !p.IsInterface);
List<ClassNameController> myControllerList = new List<ClassNameController>();
foreach (System.Type type in types)
{
// Get a PropertyInfo of specific property type(T).GetProperty(....)
PropertyInfo propertyInfo;
propertyInfo = type
.GetProperty("className", BindingFlags.Public | BindingFlags.Static);
// Use the PropertyInfo to retrieve the value from the type by not passing in an instance
object value = propertyInfo.GetValue(null, null);
// Cast the value to the desired type
string typedValue = (string)value;
myControllerList.Add(new ClassNameController(typedValue, type));
}
return myControllerList;
}
}
All of these classes got a public static string className Property. The Value of this Property I use to create an ClassNameController Instance
class ClassNameController
{
public string Name { get; set; }
public System.Type ObjectType { get; set; }
public ClassNameController(string name, Type objectType)
{
this.Name = name;
this.ObjectType = objectType;
}
public override string ToString()
{
return Name;
}
}
But when i start my Program it crashes at
object value = propertyInfo.GetValue(null, null);
with the error message
System.NullReferenceException.
Question: So why cant he find the Property Classname?
Edit:
All Classes are implementing these interfaces are WPF UserControls.
For example IModuleview:
internal interface IModuleView
{
void updateShownInformation();
void setLanguageSpecificStrings();
}
And here an example of a Module:
public partial class DateBox : UserControl, IModuleView
{
public static string className = "Datebox";
public DateBox()
{
InitializeComponent();
}
public void setLanguageSpecificStrings()
{
this.ToolTip = DateTime.Now.ToString("dddd, dd.MM.yy");
}
public void updateShownInformation()
{
tbDate.Text = DateTime.Now.ToString("ddd-dd");
}
}
So why cant he find the Property Classname?
Looking at the declaration in your posted class DateBox:
public static string className = "Datebox";
It has the signature of a field
Hence you should use the GetField method:
object value = type.GetField("className",
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Static).GetValue(null);
Explanation: What is the difference between a Field and a Property in C#?

C# Traverse object hierarchy for execution of method

I have a Case object that in itself contains a number of objects (see below), that again contains objects.
These objects can inherit from a BaseDto class, that implements the ValidateObject method, that executes a DataAnnotation validator on the current object, thus returning with validation errors, accumulated into a ValidationResult collection.
I would like to transform the following cluttered syntax, to something where I can traverse the object hierarchy of a given object, and for each of the objects (and its children!) that implements ValidateObject, execute it.
I'm feeling kinda stuck at the moment, so I would be grateful for any ideas.
cCase.ValidateObject() &
cCase.Object1.ValidateObject() &
cCase.Object2.ValidateObject() &
cCase.Object3.ValidateObject() &
cCase.Object3.ChildObject1.ValidateObject() &
cCase.Object3.ChildObject2.ValidateObject() &
cCase.Object3.ChildObject3.ValidateObject() &
cCase.Object3.ChildObject4.ValidateObject() &
cCase.Object3.ChildObject4.ChildChildObject1.ValidateObject() &
cCase.Object3.ChildObject5.ValidateObject() &
cCase.Object4.ValidateObject() &
cCase.Object4.ChildObject6.ValidateObject();
I'd use extension methods like this:
public interface IValidatable
{
bool ValidateObject();
}
public static class ValidateExtensions
{
public static bool ValidateAll(this IValidatable item)
{
if (!item.ValidateObject())
return false;
const BindingFlags flags = BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public;
var type = item.GetType();
var props = type.GetProperties(flags).Select(x => x.GetValue(item));
var fields = type.GetFields(flags).Select(x => x.GetValue(item));
return props
.Concat(fields)
.OfType<IValidatable>()
.Select(x => x.ValidateAll())
.All(x => x);
}
}
You need to get all fields, which inherit from a BaseDto class, using reflection:
public abstract class BaseDto
{
public abstract bool ValidateObject();
}
// Sample class without nested fields, requiring validation.
public class Case2 : BaseDto
{
public override bool ValidateObject()
{
Console.WriteLine("Validated: " + ToString());
return false;
}
}
// Sample nested class with nested fields, requiring validation.
public class Case1 : BaseDto
{
private Case2 Object1 = new Case2();
private Case2 Object2 = new Case2();
private Stream stream = new MemoryStream();
public override bool ValidateObject()
{
Console.WriteLine("Validated: " + ToString());
return true;
}
}
public class Case : BaseDto
{
private Case1 Object1 = new Case1();
private Case2 Object2 = new Case2();
// don't touch this field
private Stream stream = new MemoryStream();
private Case1 Object3 = new Case1();
public override bool ValidateObject()
{
Console.WriteLine("Validated: " + ToString());
return true;
}
public bool ValidateAll()
{
if (!ValidateObject()) return false;
return ValidateAll(this);
}
private bool ValidateAll(object o)
{
foreach (FieldInfo fieldInfo in o.GetType().GetFields(BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public))
{
BaseDto current = fieldInfo.GetValue(o) as BaseDto;
if(current != null)
{
bool result = current.ValidateObject();
if(!result)
{
return false;
}
return ValidateAll(current);
}
}
return true;
}
}
Call ValidateAll() on root object. Inside it traverses all fields, which are derived from BaseDto, validates them and all fields inside them recursively.
It would be cleaner to have each class in the hierarchy override ValidateObject() to validate its child objects:
public override bool ValidateObject()
{
return base.ValidateObject() &
this.Object1.ValidateObject() &
this.Object2.ValidateObject() &
this.Object3.ValidateObject() &
this.Object4.ValidateObject();
}
etc. Then the client can just call ValidateObject() on the root object.

How to use protobuf-net with immutable value types?

Suppose I have an immutable value type like this:
[Serializable]
[DataContract]
public struct MyValueType : ISerializable
{
private readonly int _x;
private readonly int _z;
public MyValueType(int x, int z)
: this()
{
_x = x;
_z = z;
}
// this constructor is used for deserialization
public MyValueType(SerializationInfo info, StreamingContext text)
: this()
{
_x = info.GetInt32("X");
_z = info.GetInt32("Z");
}
[DataMember(Order = 1)]
public int X
{
get { return _x; }
}
[DataMember(Order = 2)]
public int Z
{
get { return _z; }
}
public static bool operator ==(MyValueType a, MyValueType b)
{
return a.Equals(b);
}
public static bool operator !=(MyValueType a, MyValueType b)
{
return !(a == b);
}
public override bool Equals(object other)
{
if (!(other is MyValueType))
{
return false;
}
return Equals((MyValueType)other);
}
public bool Equals(MyValueType other)
{
return X == other.X && Z == other.Z;
}
public override int GetHashCode()
{
unchecked
{
return (X * 397) ^ Z;
}
}
// this method is called during serialization
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("X", X);
info.AddValue("Z", Z);
}
public override string ToString()
{
return string.Format("[{0}, {1}]", X, Z);
}
}
It works with BinaryFormatter or DataContractSerializer but when I try to use it with protobuf-net (http://code.google.com/p/protobuf-net/) serializer I get this error:
Cannot apply changes to property
ConsoleApplication.Program+MyValueType.X
If I apply setters to the properties marked with DataMember attribute it will work but then it breaks immutability of this value type and that's not desirable to us.
Does anyone know what I need to do to get it work? I've noticed that there's an overload of the ProtoBu.Serializer.Serialize method which takes in a SerializationInfo and a StreamingContext but I've not use them outside of the context of implementing ISerializable interface, so any code examples on how to use them in this context will be much appreciated!
Thanks,
EDIT: so I dug up some old MSDN article and got a better understanding of where and how SerializationInfo and StreamingContext is used, but when I tried to do this:
var serializationInfo = new SerializationInfo(
typeof(MyValueType), new FormatterConverter());
ProtoBuf.Serializer.Serialize(serializationInfo, valueType);
it turns out that the Serialize<T> method only allows reference types, is there a particular reason for that? It seems a little strange given that I'm able to serialize value types exposed through a reference type.
Which version of protobuf-net are you using? If you are the latest v2 build, it should cope with this automatically. In case I haven't deployed this code yet, I'll update the download areas in a moment, but essentially if your type is unadorned (no attributes), it will detect the common "tuple" patten you are using, and decide (from the constructor) that x (constructor parameter)/X (property) is field 1, and z/Z is field 2.
Another approach is to mark the fields:
[ProtoMember(1)]
private readonly int _x;
[ProtoMember(2)]
private readonly int _z;
(or alternatively [DataMember(Order=n)] on the fields)
which should work, depending on the trust level. What I haven't done yet is generalise the constructor code to attributed scenarios. That isn't hard, but I wanted to push the basic case first, then evolve it.
I've added the following two samples/tests with full code here:
[Test]
public void RoundTripImmutableTypeAsTuple()
{
using(var ms = new MemoryStream())
{
var val = new MyValueTypeAsTuple(123, 456);
Serializer.Serialize(ms, val);
ms.Position = 0;
var clone = Serializer.Deserialize<MyValueTypeAsTuple>(ms);
Assert.AreEqual(123, clone.X);
Assert.AreEqual(456, clone.Z);
}
}
[Test]
public void RoundTripImmutableTypeViaFields()
{
using (var ms = new MemoryStream())
{
var val = new MyValueTypeViaFields(123, 456);
Serializer.Serialize(ms, val);
ms.Position = 0;
var clone = Serializer.Deserialize<MyValueTypeViaFields>(ms);
Assert.AreEqual(123, clone.X);
Assert.AreEqual(456, clone.Z);
}
}
Also:
it turns out that the Serialize method only allows reference types
yes, that was a design limitation of v1 that related to the boxing model etc; this no longer applies with v2.
Also, note that protobuf-net doesn't itself consume ISerializable (although it can be used to implement ISerializable).
The selected answer didn't work for me since the link is broken and I cannot see the MyValueTypeViaFields code.
In any case I have had the same exception No parameterless constructor found for my class:
[ProtoContract]
public class FakeSimpleEvent
: IPersistableEvent
{
[ProtoMember(1)]
public Guid AggregateId { get; }
[ProtoMember(2)]
public string Value { get; }
public FakeSimpleEvent(Guid aggregateId, string value)
{
AggregateId = aggregateId;
Value = value;
}
}
when deserializing it with the following code:
public class BinarySerializationService
: IBinarySerializationService
{
public byte[] ToBytes(object obj)
{
if (obj == null) throw new ArgumentNullException(nameof(obj));
using (var memoryStream = new MemoryStream())
{
Serializer.Serialize(memoryStream, obj);
var bytes = memoryStream.ToArray();
return bytes;
}
}
public TType FromBytes<TType>(byte[] bytes)
where TType : class
{
if (bytes == null) throw new ArgumentNullException(nameof(bytes));
var type = typeof(TType);
var result = FromBytes(bytes, type);
return (TType)result;
}
public object FromBytes(byte[] bytes, Type type)
{
if (bytes == null) throw new ArgumentNullException(nameof(bytes));
int length = bytes.Length;
using (var memoryStream = new MemoryStream())
{
memoryStream.Write(bytes, 0, length);
memoryStream.Seek(0, SeekOrigin.Begin);
var obj = Serializer.Deserialize(type, memoryStream);
return obj;
}
}
}
being called like var dataObject = (IPersistableEvent)_binarySerializationService.FromBytes(data, eventType);
My message class FakeSimpleEvent has indeed parameterless constructor because I want it immutable.
I am using protobuf-net 2.4.0 and I can confirm that it supports complex constructors and immutable message classes. Simply use the following decorator
[ProtoContract(SkipConstructor = true)]
If true, the constructor for the type is bypassed during
deserialization, meaning any field initializers or other
initialization code is skipped.
UPDATE 1: (20 June 2019)
I don't like polluting my classes with attributes that belong to protobuffer because the domain model should be technology-agnostic (other than dotnet framework's types of course)
So for using protobuf-net with message classes without attributes and without parameterless constructor (i.e: immutable) you can have the following:
public class FakeSimpleEvent
: IPersistableEvent
{
public Guid AggregateId { get; }
public string Value { get; }
public FakeSimpleEvent(Guid aggregateId, string value)
{
AggregateId = aggregateId;
Value = value;
}
}
and then configure protobuf with the following for this class.
var fakeSimpleEvent = RuntimeTypeModel.Default.Add(typeof(FakeSimpleEvent), false);
fakeSimpleEvent.Add(1, nameof(FakeSimpleEvent.AggregateId));
fakeSimpleEvent.Add(2, nameof(FakeSimpleEvent.Value));
fakeSimpleEvent.UseConstructor = false;
This would be the equivalent to my previous answer but much cleaner.
PS: Don't mind the IPersistableEvent. It's irrelevant for the example, just a marker interface I use somewhere else

C# DataContract Serialization, how to deserialize to already existing instance

I have a class, which holds a static dictionary of all existing instances, which are defined at compile time.
Basically it looks like this:
[DataContract]
class Foo
{
private static Dictionary<long, Foo> instances = new Dictionary<long, Foo>();
[DataMember]
private long id;
public static readonly Foo A = Create(1);
public static readonly Foo B = Create(2);
public static readonly Foo C = Create(3);
private static Foo Create(long id)
{
Foo instance = new Foo();
instance.id = id;
instances.Add(instance);
return instance;
}
public static Foo Get(long id)
{
return instances[id];
}
}
There are other fields, and the class is derived, but this doesn't matter for the problem.
Only the id is serialized. When an instance of this type is deserialized, I would like to get the instance that has been created as the static field (A, B or C), using Foo.Get(id) instead of getting a new instance.
Is there a simple way to do this? I didn't find any resources which I was able to understand.
During deserialization it (AFAIK) always uses a new object (FormatterServices.GetUninitializedObject), but to get it to substitute the objects after deserialization (but before they are returned to the caller), you can implement IObjectReference, like so:
[DataContract]
class Foo : IObjectReference { // <===== implement an extra interface
object IObjectReference.GetRealObject(StreamingContext ctx) {
return Get(id);
}
...snip
}
done... proof:
static class Program {
static void Main() {
Foo foo = Foo.Get(2), clone;
DataContractSerializer ser = new DataContractSerializer(typeof(Foo));
using (MemoryStream ms = new MemoryStream()) { // clone it via DCS
ser.WriteObject(ms, foo);
ms.Position = 0;
clone = (Foo)ser.ReadObject(ms);
}
Console.WriteLine(ReferenceEquals(foo, clone)); // true
}
}
Note there are some extra notes on this for partial trust scenarios on MSDN, here.
I had a similar problem and the best solution that I found was adding some wrapper class, that was managing instances of the one needed to be serialized.
I am not sure about the exact signature with Contracts. I used SerializableAttribute, and with it i looked smth. like that:
[Serializable]
class FooSerializableWrapper : ISerializable
{
private readonly long id;
public Foo Foo
{
get
{
return Foo.Get(id);
}
}
public FooSerializableWrapper(Foo foo)
{
id = foo.id;
}
protected FooSerializableWrapper(SerializationInfo info, StreamingContext context)
{
id = info.GetInt64("id");
}
void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("id", id);
}
}
You may be able to get a step towards what you are looking for using OnDeserializingAttribute. However, that will probably only let you set properties (so you could have what amounts to a Copy method that populates all the properties of the current instance using your static instance.
I think if you actually want to return your static instances, you'd probably have to write your own Deserializer...
Untested, but I would assume you could implement a deserializer pretty easily like this:
public class MyDeserializer : System.Xml.Serialization.XmlSerializer
{
protected override object Deserialize(System.Xml.Serialization.XmlSerializationReader reader)
{
Foo obj = (Foo)base.Deserialize(reader);
return Foo.Get(obj.id);
}
}
Note that you'll have to do something about getting the ID since it is private in your code; Also this assumes you are using XML serialization; Replace the inheritance with whatever you actually are using. And finally, this means you'll have to instantiate this type when deserializing your objects, which may involve changing some code and/or configuration.
No problem, just use 2 classes. In getObject method you get your existing object
[Serializable]
public class McRealObjectHelper : IObjectReference, ISerializable
{
Object m_realObject;
virtual object getObject(McObjectId id)
{
return id.GetObject();
}
public McRealObjectHelper(SerializationInfo info, StreamingContext context)
{
McObjectId id = (McObjectId)info.GetValue("ID", typeof(McObjectId));
m_realObject = getObject(id);
if(m_realObject == null)
return;
Type t = m_realObject.GetType();
MemberInfo[] members = FormatterServices.GetSerializableMembers(t, context);
List<MemberInfo> deserializeMembers = new List<MemberInfo>(members.Length);
List<object> data = new List<object>(members.Length);
foreach(MemberInfo mi in members)
{
Type dataType = null;
if(mi.MemberType == MemberTypes.Field)
{
FieldInfo fi = mi as FieldInfo;
dataType = fi.FieldType;
} else if(mi.MemberType == MemberTypes.Property){
PropertyInfo pi = mi as PropertyInfo;
dataType = pi.PropertyType;
}
try
{
if(dataType != null){
data.Add(info.GetValue(mi.Name, dataType));
deserializeMembers.Add(mi);
}
}
catch (SerializationException)
{
//some fiels are missing, new version, skip this fields
}
}
FormatterServices.PopulateObjectMembers(m_realObject, deserializeMembers.ToArray(), data.ToArray());
}
public object GetRealObject( StreamingContext context )
{
return m_realObject;
}
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
}
}
public class McRealObjectBinder: SerializationBinder
{
String assemVer;
String typeVer;
public McRealObjectBinder(String asmName, String typeName)
{
assemVer = asmName;
typeVer = typeName;
}
public override Type BindToType( String assemblyName, String typeName )
{
Type typeToDeserialize = null;
if ( assemblyName.Equals( assemVer ) && typeName.Equals( typeVer ) )
{
return typeof(McRealObjectHelper);
}
typeToDeserialize = Type.GetType( String.Format( "{0}, {1}", typeName, assemblyName ) );
return typeToDeserialize;
}
}
Then, when deserialize:
BinaryFormatter bf = new BinaryFormatter(null, context);
bf.Binder = new McRealObjectBinder(YourType.Assembly.FullName, YourType.FullName);
bf.Deserialize(memStream);

Categories

Resources