Extending a class by namespace
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
精彩评论