Git Product home page Git Product logo

Comments (12)

inethui avatar inethui commented on June 17, 2024 1

Thank you so much! It has been very helpful.

from aqlaserializer.

AqlaSolutions avatar AqlaSolutions commented on June 17, 2024

What have you tried already?

from aqlaserializer.

inethui avatar inethui commented on June 17, 2024

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.

AqlaSolutions avatar AqlaSolutions commented on June 17, 2024

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.

AqlaSolutions avatar AqlaSolutions commented on June 17, 2024
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.

inethui avatar inethui commented on June 17, 2024

Thanks a lot. This is a great solution. Can "DynamicType = true" be defined through method calls?

from aqlaserializer.

AqlaSolutions avatar AqlaSolutions commented on June 17, 2024
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.

inethui avatar inethui commented on June 17, 2024

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.

AqlaSolutions avatar AqlaSolutions commented on June 17, 2024

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.

AqlaSolutions avatar AqlaSolutions commented on June 17, 2024

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.

AqlaSolutions avatar AqlaSolutions commented on June 17, 2024

See also this https://github.com/AqlaSolutions/AqlaSerializer/wiki/Security#type-whitelisting

from aqlaserializer.

AqlaSolutions avatar AqlaSolutions commented on June 17, 2024

With the latest release such interface is automatically marked as a dynamic type

from aqlaserializer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.