Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
967 views
in Technique[技术] by (71.8m points)

.net - why does my C# client that uses Library A need to have a using statement for Library B (which A uses)

I have:

  • Main Program Class - uses Library A
  • Library A - has partial classes which mix in methods from Library B
  • Library B - mix in methods & interfaces

So in Library B when I include a partial Node class which implements INode (defined in Library B) I suddenly get an error in my main class where it uses Node from Library A. The error tells me in the Main Class I have to have a using statement to Library B.

Any ideas?

EDIT - Except from code

    // *** PROGRAM ***
    class Program
    {
        static void Main(string[] args)
        {
            var context = new Model1Container();
            Node myNode;  // ** WITHOUT A using for Library B I have an error here ***
         }
     }


// ** LIBRARY A
namespace TopologyDAL
{
    public partial class Node
    {
        // Auto generated from EF
    }

    public partial class Node : INode<int>   // to add extension methods from Library B
    {
        public int Key
    }
}

// ** LIBRARY B
namespace ToplogyLibrary
{
    public static class NodeExtns
    {
        public static void FromNodeMixin<T>(this INode<T> node) {
           // XXXX
        }
    }
    public interface INode<T> 
    {
        // Properties
        T Key { get; }

        // Methods
    }

}

EDIT 2 - To clarify was it a reference or using error:

So the error that appears against the "Node myNode;" line is:

Error 1 The type 'Topology.INode`1' is defined in an assembly that is not referenced. You must add a reference to assembly 'Topology, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. U:My DropboxsourceToplogyLibraryTopologyDAL_ConsoleTestProgram.cs 11 13 TopologyDAL_ConsoleTest

When I let VS fix it for me it adds the Library2 as a reference. That is there is no "using " either before or after in the client code. So the issue is a Reference not Using issue.

EDIT 3 - Not specifically about the question, however I now notice that in the program project, I can't see the mixin methods (from Library B) unless I have a using statement to Library B? I might create a separate question re this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If LibraryA exposes properties, returns objects from methods, or take parameters of types that are defined in LibraryB, and you declare or use a variable in Main Program of one of those types, you'll need the using statement.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...