How to serialize non-static child class of static class

前端 未结 3 1592
-上瘾入骨i
-上瘾入骨i 2021-01-04 12:10

I want to serialize a pretty ordinary class, but the catch is it\'s nested in a static class like this:

public static class StaticClass
{
    [Serializable]
         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-04 12:45

    As a pragmatic workaround - don't mark the nesting type static:

    public class ContainerClass
    {
        private ContainerClass() { // hide the public ctor
            throw new InvalidOperationException("no you don't");
        }
    
        public class SomeType
        {
            ...
        }
    }
    

提交回复
热议问题