Nested Partial Class

后端 未结 2 1859
挽巷
挽巷 2021-02-18 16:07
internal partial class Class1
{
    private class Class2 : ISomething, ISomethingElse
    {
        private class Class3 : ISomething
        {

        }
    }
}
         


        
2条回答
  •  感情败类
    2021-02-18 16:53

    If a nested class needs to be for any reason partitioned then also the owner class needs to be partitioned. Nested class code IS also the owner class code - it is a composition relation.

    Thus extracting part of a nested class means also extracting part of its owner at the same time. In turn we need to "see full path" to each partial class so that compiler can identify the fully specified type.

    It is the same with namespaces - unlike classes they are somehow partial implicitly, because we do not expect having whole namespace in the same file, while we normally do so for classes, especially the nested ones.

    So normally we need to write in a file with parts of our nested classes

    MyTopNamespace.MyLevel2Namespace.MyTopLevelClass.MyNestedClassA
    MyTopNamespace.MyLevel2Namespace.MyTopLevelClass.MyNestedClassB
    

    defined something like:

    namespace MyTopNamespace 
    { 
        namespace MyLevel2Namespace 
        {
            partial class MyTopLevelClass
            {
                partial class MyNestedClassA
                {
                    // Part of definition for our nested class:
                    // MyTopNamespace.MyLevel2Namespace.MyTopLevelClass.MyNestedClassA
                }
                class MyOtherNestedClassNotPartitioned
                {
                   ...
                }
                partial class MyNestedClassB
                {
                    // Part of definition for our nested class:
                    // MyTopNamespace.MyLevel2Namespace.MyTopLevelClass.MyNestedClassB
                }
            }
        }
    }
    

    and in another file with other part of nested class suffixed 'A' defined something like:

    namespace MyTopNamespace 
    { 
        namespace MyLevel2Namespace 
        {
            partial class MyTopLevelClass
            {
                partial class MyNestedClassA
                {
                   // Another part of definition for our nested class:
                   // MyTopNamespace.MyLevel2Namespace.MyTopLevelClass.MyNestedClassA
                }
           }
        }
    }
    

    and in yet another file with other part of nested class suffixed 'B' defined something like:

    namespace MyTopNamespace 
    { 
        namespace MyLevel2Namespace 
        {
            partial class MyTopLevelClass
            {
                partial class MyNestedClassB
                {
                   // Another part of definition for our nested class:
                   // MyTopNamespace.MyLevel2Namespace.MyTopLevelClass.MyNestedClassB
                }
           }
        }
    }
    

    Or we can have other files with parts of both nested classes defined etc., but we always need to fully specify where is the class type defined.

提交回复
热议问题