Enum EnumPassthru enabled by default

The quick question is, can EnumPassthru be enabled by default for all types of enums? For now, I have to enable this for each type of enumeration manually or use the following method to automatically apply it to my DTO assembly types:

public static void ConfigureEnumTypes(RuntimeTypeModel tm, Assembly a) { foreach (var type in a.GetTypes()) { if (type.IsEnum && type.GetCustomAttribute<ProtoContractAttribute>() != null) tm[type].EnumPassthru = true; } } 

If there is a better way, I would like to know. Thanks.

+6
source share
1 answer

This goes into the following assembly:

 [TestFixture] public class SO17245073 { [Test] public void Exec() { var model = TypeModel.Create(); Assert.IsFalse(model[typeof(A)].EnumPassthru, "A"); Assert.IsTrue(model[typeof(B)].EnumPassthru, "B"); Assert.IsFalse(model[typeof(C)].EnumPassthru, "C"); Assert.IsTrue(model[typeof(D)].EnumPassthru, "D"); Assert.IsTrue(model[typeof(E)].EnumPassthru, "E"); Assert.IsTrue(model[typeof(F)].EnumPassthru, "F"); Assert.IsFalse(model[typeof(G)].EnumPassthru, "G"); Assert.IsFalse(model[typeof(H)].EnumPassthru, "H"); } // no ProtoContract; with [Flags] is pass-thru, else not public enum A { X, Y, Z } [Flags] public enum B { X, Y, Z } // basic ProtoContract; with [Flags] is pass-thru, else not [ProtoContract] public enum C { X, Y, Z } [ProtoContract, Flags] public enum D { X, Y, Z } // ProtoContract with explicit pass-thru enabled; always pass-thru [ProtoContract(EnumPassthru = true)] public enum E { X, Y, Z } [ProtoContract(EnumPassthru = true), Flags] public enum F { X, Y, Z } // ProtoContract with explicit pass-thru disabled; never pass-thru // (even if [Flags]) [ProtoContract(EnumPassthru = false)] public enum G { X, Y, Z } [ProtoContract(EnumPassthru = false), Flags] public enum H { X, Y, Z } } 

From your sample code here:

 if (type.IsEnum && type.GetCustomAttribute<ProtoContractAttribute>() != null) 

this looks like all you have to do, since you already have [ProtoContract] , do this [ProtoContract(EnumPassthru = true)] in your enum ads.

+5
source

Source: https://habr.com/ru/post/947877/


All Articles