开发者

Extending a class by namespace

开发者 https://www.devze.com 2023-02-17 16:27 出处:网络
I have a class within a library, 开发者_Python百科with no root namespace, firstone.dll: namespace first

I have a class within a library, 开发者_Python百科with no root namespace, firstone.dll:

namespace first
    public partial class one
        public sub fun()

        end sub
    end class
end namespace

My second library, with no root namespace, firstonetwo.dll, references firstone.dll:

namespace first.one
   public partial class two
       public sub testfun()
            first.one.fun() 'not recognized'
       end sub
   end class
end namespace

or

namespace first
   public partial class one
        public partial class two
            public sub testfun()
                 first.one.fun() 'also not recognized'
            end sub
        end class
   end class
end namespace

Is there a way to extend the class in a separate dll and still have access to the original class? I don't want to inherit the class just extend it.


The problem is that you have a namespace and a class with the same name and that causes a lot of confusion. Your first example is a class called one in the first namespace and your second example is a class called two in the first.one namespace. Just because they can be written the same, there's big difference between the two.

Do not name a class the same as its namespace, Part One

EDIT

Your tree looks something like this:

  • first (ns)
    • one (class)
    • fun (method)
    • one (ns)
    • two (class)

VB is having a problem walking the tree and determining which one to look at.


Take a look at "Extension Methods" http://msdn.microsoft.com/en-us/library/bb384936.aspx

This is the only form of 'extension' possible without inheriting.


This is untested

namespace first 
    public partial class one 
        public overridable sub fun() 

        end sub 
    end class 
end namespace

Separate CLASS File

Imports first
namespace one 
   public partial class fun 
        public overrides sub fun() 

        end sub 
   end class 
end namespace 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号