1

Why do I get this problem?

Background: myClass inherits from a class ABC which inherits from a BaseClass. BaseClass is a custom created class in a different namespace and contained in a different DLL assembly.

Issue: VisualStudio wants me to add a reference to the BaseClass DLL - otherwise it won't compile myClass.

  • Why don't the compiler make use of the metadata?
  • What if I bought the DLL (possibly without source code) and the vendor uses hundreds of classes to inherit from, why would I need to add (and know) all those references?

Thanks

Matt
  • 25,467
  • 18
  • 120
  • 187
ejuarez
  • 19
  • 2
  • What is the BaseClass? Is it your other custom created class? Is it from the same namespace? Or is it in a spearate project? Separate assembly perhaps? – Sebastian Zaklada Mar 18 '11 at 23:47
  • It is a custom created class in a different namespace and contained in a different dll assembly – ejuarez Mar 24 '11 at 16:18
  • Not sure if you have noticed, but in some cases - e.g. static properties - you don't need those references. Look here: ["Why can the C# compiler see static properties but not instance methods?"](https://stackoverflow.com/q/51547053/1016343) – Matt Aug 02 '18 at 08:19

4 Answers4

3

How could it get the metadata for the base class if you don't provide it? It doesn't get copied from the base class to the derived class and stored in the derived class' assembly.

Your vendor scenario is just not realistic. Nobody designs class hierarchies that are a hundred classes deep, let alone store each of them in a separate assembly. This tops out at six or seven, at worst. With one base assembly, sometimes two. Any deeper and nobody can understand how it works anymore.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I thought metadata was created when you create the assembly, baseclass is in a separate dll assembly. Quoting from wikipedia article "...Metadata describes all classes and class members that are defined in the assembly, and the classes and class members that the current assembly will call from another assembly. The metadata for a method contains the complete description of the method, including the class (and the assembly that contains the class), the return type and all of the method parameters..." – ejuarez Mar 24 '11 at 16:18
  • 1
    Not sure what the point of the quote might be. Wikipedia isn't going to compile your code. Post to connect.microsoft.com if you think it should work differently. – Hans Passant Mar 24 '11 at 16:23
  • @ejuarez - I don't know what you want us to tell here, but it seems to me that it is not much related with the question. – Matt Aug 02 '18 at 14:54
2

Regarding your vendor scenario - you won't be forced to reference all the libraries from vendor's library if that library doesn't expose anything from other libs and all those libs are used only internally.

But if a library for example returns a class from another library (or it's inheritor) then of course you'll have to reference both. Just in order to get all the information about those exposed classes.

Snowbear
  • 16,924
  • 3
  • 43
  • 67
  • How about inheritance? The question was about a base class which resides in a separate DLL. In this case, the class **does** expose something in the metadata: the name of the base class! – Matt Aug 02 '18 at 11:08
2

If BaseClass isn't referenced, how would your program know about it and its definition? Where is it supposed to grab the meta data from if it has no reference to it?

It's like asking a web server-side related question on SO but not telling anyone what language/platform you're using - there's no frame of reference.

Kon
  • 27,113
  • 11
  • 60
  • 86
  • 1
    If A depends on B, and B depends on C, I can appreciate that A depends on C. But if A references B, and B references C, why do I need to tell Visual Studio to have A REFERENCE C? Why can't it just do a deep reference for me? As an option? Somewhere? If B is consumed by dozens of projects, I have to add a reference to C to each and every one of them. – Matt Cruikshank Jul 20 '12 at 23:04
1

Let's answer your questions...

In order to compile the code for MyClass, it needs to know what Class did. In order to know what Class did, it needs to see what BaseClass did - it's like a chain.

You need to include anything you are building against in your project. This can be done by either actually having the source for whatever in your project, or by including a reference to the project / binary where the behaviour is defined.

Meta data doesn't contain any details about the actual logic in a class.

Martin Milan
  • 6,346
  • 2
  • 32
  • 44