locked
Problem with inheritance and references RRS feed

  • Question

  • Hi everybody,

    We are developping an application with Visual Studio 2008 (an WPF application with some Class' Libraries in C#). For that, we use some other components developed by another company. We don't want to use these components directly and we want to enrich with new features. For that, we created base classes that inherit directly from class of these components. Only it seems that we have to reference them all (the basic components and our base classes) to use our base classes or we get an error message when compiling :

    The type 'Type' is defined in an assembly that is not referenced. You must add a reference to assembly.

    Here is a quick simple example to show you more clearly our problem :

    We have a solution with 3 projects (an WPF and 2 class' libraries) :

    The first class library represents the components developed by the other company :


    namespace ClassLibrary1
    {
        public class Class1
        {
            public string Test()
            {
                return "Test !";
            }
        }
    }
    The second class library reprsents our base classes :

    using ClassLibrary1;
    
    namespace ClassLibrary2
    {
        public class Class2 : Class1
        {
            public new string Test()
            {
                return string.Concat("Class 2 : ", base.Test());
            }
        }
    }
    At least, the WPF applicvation has just a button with a "Click" event. Here is the code-behind :

    using ClassLibrary2;
    
    namespace WpfApplication1
    {
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                Class2 obj = new Class2();
    
                MessageBox.Show(obj.Test());
            }
        }
    }
    Our problem is that it seems we must referencing the two class libraries' projects in our "WpfApplication1" project. But we only want to reference the " ClassLibrary2" class library.

    Someone there has a solution ?

    Thank you. Have a nice day.

    Wednesday, March 24, 2010 7:44 AM

Answers

  • You see - actually in low level details every details re-written as fully qualified name. You writing namespaces, classes, structs and variables but actually it converted to:

     

    using Namespce4

    {

       private System.Int32 myVar1;

       public Namespace1.Namespace2.Namespace3.Namespace4.Class1.ctor()

      {

        some code here

         System.Int32.myVar1 = 12;

        Namespace1.Namespace2.Namespace3.Namespace4.CallingFunction(12);

      }

    }

     

     You see - .NET always flatten it with fully qualified name even Int and String, etc... So you can not make him guess for type - it must know it before executing. If .NET un-sure (even for 0.1%) what is this type of where to load the type - it will throw Exception. Or app can load the same type from 2 different places - it will throw exception - it must be crystal clear what is this type, where it can load it from and other little details - such as size, meta-data, etc...

     


    C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off (c) — Bjarne Stroustrup [http://www2.research.att.com/~bs/bs_faq.html#really-say-that]
    • Proposed as answer by Jasper22 Wednesday, March 24, 2010 3:39 PM
    • Marked as answer by Niki1291 Thursday, March 25, 2010 9:15 AM
    Wednesday, March 24, 2010 3:39 PM
  • Hi Niki1291,

    I think Japer22 is right, since ClassLibrary2 base on ClassLibrary1, if you refer to ClassLibrary2, it will also add ClassLibrary1 dll. Unless you don't let ClassLibrary2 inherit from ClassLibrary1.

     

    Best regards,
    Guang-Ming Bian - MSFT


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    • Marked as answer by Niki1291 Thursday, March 25, 2010 9:15 AM
    Thursday, March 25, 2010 9:13 AM

All replies

  • It seems that the problem not in code but in error 'The type 'Type' is defined in an assembly that is not referenced. You must add a reference to assembly.'  Could you check it again ? You can use NDepend or fusion to see what your assembly reference/load at run-time

    C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off (c) — Bjarne Stroustrup [http://www2.research.att.com/~bs/bs_faq.html#really-say-that]
    • Proposed as answer by Jasper22 Wednesday, March 24, 2010 8:14 AM
    Wednesday, March 24, 2010 8:13 AM
  • Yes, it's a reference's problem. But why do I need to reference the ClassLibrary1 in the WpfApplication1 ? The WpfApplication1 doesn't use directly the ClassLibrary1, the ClassLibrary2 do that. Doesn't it ?

    And I try the NDepend software. Here is what it "says" about Assemblies Dependencies :

    Assembly Depends on... Is referenced by...
    ClassLibrary1 v1.0.0.0 mscorlib v2.0.0.0 ; ClassLibrary2 v1.0.0.0 ;
    ClassLibrary2 v1.0.0.0 ClassLibrary1 v1.0.0.0 ; mscorlib v2.0.0.0 ; WpfApplication1 v1.0.0.0 ;
    WpfApplication1 v1.0.0.0 System v2.0.0.0 ; PresentationFramework v3.0.0.0 ; mscorlib v2.0.0.0 ; WindowsBase v3.0.0.0 ; PresentationCore v3.0.0.0 ; ClassLibrary2 v1.0.0.0 ; -
    Wednesday, March 24, 2010 2:33 PM
  • You see - actually in low level details every details re-written as fully qualified name. You writing namespaces, classes, structs and variables but actually it converted to:

     

    using Namespce4

    {

       private System.Int32 myVar1;

       public Namespace1.Namespace2.Namespace3.Namespace4.Class1.ctor()

      {

        some code here

         System.Int32.myVar1 = 12;

        Namespace1.Namespace2.Namespace3.Namespace4.CallingFunction(12);

      }

    }

     

     You see - .NET always flatten it with fully qualified name even Int and String, etc... So you can not make him guess for type - it must know it before executing. If .NET un-sure (even for 0.1%) what is this type of where to load the type - it will throw Exception. Or app can load the same type from 2 different places - it will throw exception - it must be crystal clear what is this type, where it can load it from and other little details - such as size, meta-data, etc...

     


    C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off (c) — Bjarne Stroustrup [http://www2.research.att.com/~bs/bs_faq.html#really-say-that]
    • Proposed as answer by Jasper22 Wednesday, March 24, 2010 3:39 PM
    • Marked as answer by Niki1291 Thursday, March 25, 2010 9:15 AM
    Wednesday, March 24, 2010 3:39 PM
  • So, if I understand, I have to reference the ClassLibrary1 (see my example) in my WPF application project. There isn't any other solutions ?!
    Thursday, March 25, 2010 7:43 AM
  • Hi Niki1291,

    I think Japer22 is right, since ClassLibrary2 base on ClassLibrary1, if you refer to ClassLibrary2, it will also add ClassLibrary1 dll. Unless you don't let ClassLibrary2 inherit from ClassLibrary1.

     

    Best regards,
    Guang-Ming Bian - MSFT


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    • Marked as answer by Niki1291 Thursday, March 25, 2010 9:15 AM
    Thursday, March 25, 2010 9:13 AM