Comments (12)
Thank you so much! It has been very helpful.
from aqlaserializer.
What have you tried already?
from aqlaserializer.
This is what I've tried, I got error "A type can only participate in one inheritance hierarchy"
[Fact]
public void InheritClassAndInterface()
{
MetaType metaTypeA = RuntimeTypeModel.Default.Add(typeof(ClassA), true);
MetaType metaTypeB = RuntimeTypeModel.Default.Add(typeof(ClassB), true);
MetaType metaType1 = RuntimeTypeModel.Default.Add(typeof(Interface1), true);
metaTypeB.AddSubType(1, typeof(ClassA));
metaType1.AddSubType(1, typeof(ClassA));
RuntimeTypeModel.Default.CompileInPlace();
}
private class ClassA : ClassB, Interface1
{
}
private class ClassB
{
}
public interface Interface1
{
}
from aqlaserializer.
Yeah, it means that each type can have only one base type specified. You still can inherit both but you have to decide to use fields either of type ClassB
or of type Interface1
.
There is also another option: to enable WriteAsDynamicType
on interface-typed fields in the [SerializableMember]
attribute (while having only ClassA : ClassB
inheritance specified in the TypeModel
).
from aqlaserializer.
using AqlaSerializer;
using AqlaSerializer.Meta;
namespace ConsoleApp5
{
public class MyTest
{
public void Execute()
{
MetaType metaTypeA = RuntimeTypeModel.Default.Add(typeof(ClassA), true);
MetaType metaTypeB = RuntimeTypeModel.Default.Add(typeof(ClassB), true);
metaTypeB.AddSubType(1, typeof(ClassA));
RuntimeTypeModel.Default.CompileInPlace();
var c = new TestClass
{
ClassA = new ClassA { Value1 = 1, Value2 = 2, Value3 = 3 },
ClassB = new ClassB { Value2 = 22 },
Interface1 = new ClassA { Value1 = 1, Value2 = 2, Value3 = 3 },
};
var c2 = RuntimeTypeModel.Default.DeepClone(c);
}
[SerializableType]
class TestClass
{
[SerializableMember(1, DynamicType = true)]
public Interface1 Interface1 { get; set; }
[SerializableMember(2)]
public ClassB ClassB { get; set; }
[SerializableMember(3)]
public ClassA ClassA { get; set; }
}
[SerializableType]
private class ClassA : ClassB, Interface1
{
[SerializableMember(1)]
public int Value3 { get; set; }
[SerializableMember(2)]
public int Value1 { get; set; }
}
[SerializableType]
[SerializeDerivedType(1, typeof(ClassA))]
private class ClassB
{
[SerializableMember(2)]
public int Value2 { get; set; }
}
public interface Interface1
{
public int Value1 { get; set; }
}
}
}
from aqlaserializer.
Thanks a lot. This is a great solution. Can "DynamicType = true" be defined through method calls?
from aqlaserializer.
RuntimeTypeModel.Default.Add(typeof(TestClass), true)[nameof(TestClass.Interface1)].SetSettings(x => x.V.WriteAsDynamicType = true, 0);
For now it can only be set for a member.
from aqlaserializer.
This works great. What would be the downsides of this approach? Does it make sense to always mark a field as "DynamicType = true" if its type is an interface? We have more than 10k classes and interfaces. Manually configure each of them is not practical, so we always prefer to set things up programmatically.
from aqlaserializer.
Sorry, the previous post that I deleted contained wrong information.
If you need to serialize interfaces or System.Object
then yes, usually you have to mark them as dynamic type.
This feature writes full runtime type name into output stream. The name is written only once per type so there is no big size overhead even if you have multiple dynamic fields of a same type.
This is also not very safe if you receive your stream from an untrusted source. An attacker may create any of types registered in the model if you use precompiled TypeModel
or any available type with supported contract attribute or any available list type if it's executed on RuntimeTypeModel
.
Also the LateReference
feature doesn't work with DynamicType
.
from aqlaserializer.
To prevent creating non-registered types you can set RuntimeTypeModel.AutoAddMissingTypes
to false
.
More specifically this is called when a new type is encountered in an input stream:
from aqlaserializer.
See also this https://github.com/AqlaSolutions/AqlaSerializer/wiki/Security#type-whitelisting
from aqlaserializer.
With the latest release such interface is automatically marked as a dynamic type
from aqlaserializer.
Related Issues (20)
- Enum With Duplicate Items HOT 2
- Is nuget package 2.0.1 the latest version? HOT 2
- Invalid serialization operation with wire-type None HOT 5
- DeflateStream is not supported? HOT 22
- Possible recursion detected HOT 9
- RuntimeTypeModel.Default.CompileInPlace is too slow HOT 5
- Non-public member cannot be used with full dll compilation HOT 12
- AqlaSerializer.ProtoException: Trap count > 1, will be mismatched with next NoteObject HOT 15
- Dynamic type is not a contract-type
- Un-defined enum values HOT 1
- Unexpected sub-type HOT 4
- How to setup Aqla for complicated object relationships HOT 4
- Wrong group was ended
- Recursion depth exceeded safe limit HOT 2
- "Unable to determine member" when running obfuscated code HOT 1
- A deferred key does not have a value yet (NoteObject call missed?) HOT 10
- How to unregister or re-register a type? HOT 6
- Can you please publish the latest code?
- Failed to clone a dictionary with enum value
- Unable to Serialize a string object dictionary containing a string array as object
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aqlaserializer.