Finding references in Visual Studio - c#

I'm using Visual Studio and COM with C# for the first time and there's something I don't quite understand about the 'references'. How do you know what to reference with a given 'using something.something'? The .Net references seem fairly simple, but COM is less obvious. I'm running Visual Studio 2005 and have the latest VSTO installed, but for the life of me, I can't figure out what I need to reference to satisfy:
using Microsoft.VisualStudio.Tools.Applications.Runtime;
There are a few Microsoft.VisualStudio things in the .Net tab, but none that continue with .Tools or anything like it. I feel like I'm missing a key concept here.

There are two issues here -
First, a reference doesn't necessarily correspond to a namespace. A single reference can contain multiple namespaces, and a single namespace can be shared by multiple assemblies which would need to be referenced. Needing to include a reference allows you to use specific types, not entire namespaces.
Second, this is something you'll need to know in advance. If you're using Microsoft's classes, such as the ones in the namespace you listed, MSDN can be a great help.
For example, take Microsoft.VisualStudio.Tools.Applications.Runtime.ServerDocument
If you look at the MSDN page for this class (in that namespace), near the top it includes:
Namespace: Microsoft.VisualStudio.Tools.Applications.Runtime
Assembly: Microsoft.VisualStudio.Tools.Applications.Runtime (in microsoft.visualstudio.tools.applications.runtime.dll)
This specifically tells you which assembly is required.

That reference is part of the Visual Studio SDK. I am currently using VS 2008, but the assemblies should be the same for VS 2005. The link for the SDK is here.

Going the other way is pretty easy. If you're given a reference, you can open that reference in the object browser to see what namespaces it contains, and from that, determine what usings to add. Its not trivial to determine what to reference for a given using, as there's no guarantee that there exists exactly one DLL for each namespace. How is it that you arrived at a
using Microsoft.VisualStudio.Tools.Applications.Runtime;
Without knowing what to reference? If its a code sample somewhere, they ought to mention what the external references and dependencies of the project are.

Related

Configure Visual Studio Autocomplete to list classes outside of namespace?

When I type in the autocomplete/intellisense for Visual Studio, if the thing I'm trying to reference isn't part of the current namespace, it won't show it at all.
Is there a way to get the Visual Studio autocomplete to include the names of classes that are in my solution, but not necessarily in my current namespace? This would be similar to how the autocomplete for Typescript works in VS Code - it lists things that match what you type, then automatically imports them upon completion.
IntelliJ autocomplete for Java also works the way I'd like it to - when I start typing, it gives me a list of all the various things that match, including their classpath, so I can pick the one I need.
If this is a thing for Visual Studio, I can't figure out how to configure it to do this.
ReSharper you will do the job (expensive but powerful).
https://www.jetbrains.com/help/resharper/Coding_Assistance_Code_Completion_Auto.html
If you need just to extend VS intellisense you can try this one:
https://marketplace.visualstudio.com/items?itemName=Dreamescaper.IntellisenseExtender2019
With Visual Studio 2019 now there is an IntelliSense option to "Show items from unimported namespaces" to achieve similar functionality to ReSharper. You still need to have added a reference to the project though.
I believe the issue is due to a lack of a reference from one project to the other. In the project you're working on, you have to manually add a reference to the project you're trying to reach.
Right-click the project node in Solution Explorer > Add > Reference > Projects > (your project name)
Then in the code, you probably want to include a using statement to reference the namespace and classes. Something along the lines of: using YourNamespace; using YourNamespace.YourClass; and so on depending on your needs.
But with the reference added, VS should be able to find the namespaces (similar to adding an external library).
The thing to keep in mind is that IntelliJ, and the autocomplete features in VS Code work much differently than Intellisense for C#. The former basically just looks through the code for similar usages, and infers from that that it's a potential member/function of what you're referencing. You'll see this in regular text editors like Atom and whatever. Autocomplete for C# is much more involved and VS does a lot more with reading the actual code and is less about inferring. I'm guessing this was done because it's easier for them to implement just based on the nature of the languages.

How to add reference to two versions of the same api in visual studio/c#

I have a project that depends on an external program, this external program has an API, well actually it has about 17 different APIs all slightly different for version 2000-2017. Now within these 17 versions I want to support about 5 of them. (2012-2017) but there are several features that were in 2012 that have since been renamed in 2017.
Now the good news is that I can trivially determine which version of the program a given user is using and any shared functions (90%+) can be called using a reference to a different version of the API. However I need some of the remaining 10% of the features. So I need to include the references to multiple APIs so that my program will compile and then at runtime pick which version it receives.
Now what I tried is to go into the visual studio (2015 community version), and add a reference to several of thes. However the moment I try adding in a second reference I get a error message: a reference to [API.dll] could not be added a reference to the component [API.dll] already exists in project..
I would like the method use to be such that if a function with a given name exists in one of the versions it should bind to that one and if a given function name exists in multiple APIs then it should bind to the latest. Any idea how to do this? Maybe something using the extern alias keyword?
I looked at How to reference two versions of an API? and the accepted answer won' t work but the second answer might, anyone capable of explaining if that one will work and if so how to do it correctly?
Basically, it is not allowed to add multiple references with the same name.
If you are the assembly owner, you have to change the file name in the manifest to generate DLLs with different names.
You can also manage the assembly version in the configuration file or load at runtime.
My suggestion is to merge all the DLLs into a single file. You can use ILMerge to do this.

Does a Visual Studio 2012 project use a reference DLL or local file first?

I may not be asking this question correctly as I haven't been able to find a reference here or on Google. I need to modify a class that is in one of my reference DLL's. Will VS look at the reference DLL first or the local file first ? It seems to look at the reference DLL first. Is there a way to tell VS which to look at first ?
Thanks
Clarification - I have a class definition in the reference DLL and also in a local project file. The local project file is the class definition that I need the program to use.
Using an alias on your references, you can specify which of two similarly named classes you are referring to in your code.
This is explained by Jon Skeet here:
What use is the Aliases property of assembly references in Visual Studio 8
Limitations
This will only help you, if you instanciate that class yourself. It is not possible to let a foreign assembly create instances of your own class if there is no dedicated mechanism (i.e. API) for doing so.
Your question is quite vague, but if you use the Fusion Log Viewer FusLogVw.exe (just search for it on your computer) you can see what dlls are loaded and which paths were used to look for it.
This depends on how you setup your reference to the project in question. Visual Studio has 2 concepts of references.
Project to Project
Project to DLL
For the sake of this discussion lets call the main project A and the referenced project B (the class to change is located in B)
If there is a project to project reference from A to B then the build is always against the latest source code. Hence if you change the class definition A will always build against the changed class.
If there is a project to DLL reference from A to B then things are a bit murkier. There are so many possibilities here that it is hard to speculate intelligently what will happen. It may get the updated class, it may not, it may after a second rebuild.
In general though if you have 2 projects in the same solution and one references the other you should always use a project to project reference. The easiest way to guarantee this is to delete the reference, right click select add reference and go through the project route instead of "Browse".

I need help understanding how C# projects are organized in Visual Studio

I've been teaching myself C# for the past couple weeks, and as someone whose IDE of choice is Notepad, I'm having a little bit of difficulty transitioning to Visual Studio (I'm using 2010 express). In particular, I'm wondering how the organization of the Namespace-Class-Method hierarchy manifests itself VISUALLY in the interface. I'm having a hard time making sense of it, and, more importantly, how to use the interface to effectively organize and keep track of my projects.
For instance, there's the "solution explorer", but there's no such thing as a C# "solution" (that I'm aware of). I suspect it's Microsoft's marketing speak for a more generic development term, but I can't figure it out. I get the option of creating New "projects". Is a "project" a "solution"?
I'm also a little fuzzy on namespaces. I suspect that the namespaces are the equivalent of a class library in Java. What are some examples of how namespaces are used in the real world? Say, for instance, I'm developing a personal finance application. Would I put EVERYTHING related to that application in one solution? Or would I create as namespace for, say, cash accounts and a namespace for investment accounts?
Within the namespaces are my *.cs files but I can't seem to figure out how to create a NEW *.cs file in my namespace. I would EXPECT, based on the explorer hierarchy, that any class using a namespace would appear in that list, and I would be able to use it as needed. For instance, I would be able to create enterDeposits.cs and enterWithdrawals.cs WITHOUT needing to create a new project.
I've found a couple tutorials online that tell me how to do things (like creating a new project), but without a solid understanding of the IDE's vocabulary, I'm not really sure I'm keeping everything organized as well as I could. Help!
Solutions and projects in Visual Studio are ways to organize code - they are containers used by the IDE to issue commands to the compiler and other build components as needed.
Solutions contain projects - projects contain code files.
Each project will compile to a separate DLL or EXE, a unit of deployment.
Namespaces can be spread across projects and solutions and be in different DLLs/EXEs. They are units of logical separation.
In Visual Studio, you can set a base namespace for each project in its properties. By default, every directory you create will get appended to that as part of the inner namespace. Any source code file created in a directory will by default get that namespace.
In general, namespaces are a pure code construct.
In a C# file, you have a namespace declaration - this can be any valid namespace identifier and can be in any project/solution/code file.
I do suggest taking a look at MSDN - it is a good resource for anything C# and Visual Studio.
Solution and Project Basics
Creating Solutions and Projects
Your first question has already been answered here: Visual Studio Project vs. Solution
You can find plenty of information about your send question here: namespace (C# Reference)
Great overview by Oded. RE: would EXPECT, based on the explorer hierarchy, that any class using a namespace would appear in that list, and I would be able to use it as needed. you are correct, the IDE will recognize classes in the SAME namespace.
One thing that tricks up new users is that namespaces do not inherit. If you have a namespace MyProject and another namespace MyProject.SubNamespace, the compiler will not automatically link the SubNamespacetwo. You need to specify a Using statement, e.g. "Using MyProject.SubNamespace" to let the IDE know to use the classes in that namespace.

Where is my System.Numerics namespace?

I'm using Visual Studio 2010 and trying to use the BigInteger type in a C# program. This type is supposed to be available in System.Numerics namespace, but I don't seem to have that installed in the .Net 4.0 framework. When I type "using System.Numerics;" in VS2010, a red underline appears under the "Numerics". Has anyone else ever had this problem?
If so, how do you resolve it? I just re-downloaded and re-installed (repaired) the .Net 4.0 framework, but that didn't help. I've never had any problems with other C# programs that I've written, so I'm wondering what I'm missing.
You need to add an assembly reference to System.Numerics.dll
The MSDN entry on BigInteger states:
BigInteger Structure
...
Namespace: System.Numerics
Assembly: System.Numerics (in System.Numerics.dll)
Though most assemblies are the part of the installation, VS does add only some core elements to the reference list of a project (Core, Data, XML etc.). One needs to add other references by hand using Reference Manager.
Right-click your project, then select Add Reference (or Add->Reference). System.Numerics resides in Assemblies. Then you can use using System.Numerics; in project files.
PS: this answer is not intended to get any vote. It is only for future reference if someone needs how to do it, just as I needed before this answer.

Categories

Resources