using namespace in C# from CLI/C++ class - c#

This might be a C#-noob question...
Assume I have the following CLI/C++ header:
namespace DotNet {
namespace V1 {
namespace X {
public ref class AClass {
public:
AClass() {}
void foo() {}
static void bar() {}
};
}
}
}
Then from C# I do the following:
using DotNet;
V1.X.AClass a = new V1.X.AClass();
I get:
Program.cs(18,7): error CS0246: The type or namespace name 'V1' could
not be found (are you missing a using directive or an assembly
reference?)
Same with:
using DotNet.V1;
X.AClass a = new X.AClass();
Program.cs(18,7): error CS0246: The type or namespace name 'X' could
not be found (are you missing a using directive or an assembly
reference?)
What works is:
DotNet.V1.X.AClass a = new DotNet.V1.X.AClass();
Or
using DotNet.V1.X;
AClass a = new AClass();
So either I have to use the full namespace path, or I have to open all of the path to access the class. Is there anything I can do about this?
I would like to be able to open only a part of it.

So either I have to use the full namespace path, or I have to open all of the path to access the class.
Yes.
Is there anything I can do about this?
Not that I'm aware of. Why would you not just want a using directive for the whole namespace? Isn't that the simplest approach?
Note that this has nothing to do with C++/CLI. The same is true whatever the source language is - so you can see it with C# as well:
namespace DotNet.V1.X
{
public class AClass {}
}

As Jon Skeet stated you have to include the full namespace in either the using statement or each time you reference it in code. I recommend just placing the full namespace in the using statement.
You can, however, achieve that syntax with a using alias, but it does not add anything of value outside of syntax.
using V1 = DotNet.V1;
...
V1.X.AClass a = new V1.X.AClass();
Also if you are using C# version 3.0 or higher the var keyword will only require you type out the full namespace on the right side of the assignment.
var a = new DotNet.V1.X.AClass();

Related

Does anyone know why system and console isn't working here?

using System;
using SplashKitSDK;
public class Program
{
public static void Main()
{
console.WriteLine("Hello World");
}
}
Some of the errors I am getting:
Unnecessary using directive. [Helloworld]csharp(CS8019)
The type or namespace name 'System' could not be found (are you missing a
using
directive or an assembly reference?) [Helloworld, Helloworld, Helloworld,
Helloworld]csharp(CS0246)
Predefined type 'System.Object' is not defined or imported
The name 'Console' does not exist in the current context
I've tried everything, and nothing seems to work. I also believe I installed the language tools and library correctly. If anyone knows how to fix this please let me know. Thanks!

C# ArgumentNullException type or namespace could not be found. VS Code

Part Of My Code:
public void Move(Point newLocation)
{
if (newLocation == null)
throw new ArgumentNullException("newLocation");
Move(newLocation.X, newLocation.Y);
}
I get this as a Error:
The type or namespace name 'ArgumentNullException' could not be found (are you missing a using directive or an
assembly reference?)
Is there something wrong with my c# code. Could this be a VS Code C# support extension bug?
I am a beginner to c# please explain
ArgumentNullException is in the "System" namespace. So either fully-qualify the name,
throw new System.ArgumentNullException("newLocation");
or add
using System;
to the top of your C# file or your namespace.

Roslyn: Effect of CSharpCompilerOptions WithUsings

My question is about CSharpCompilerOptions and using statements passed to an instance of CSharpCompilation.
I have a syntax tree parsed from some text:
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(#"
namespace RoslynCodeAnalysis
{
using System;
using System.Net; // Commenting this out causes errors
public class Writer
{
public void Write(string message)
{
WebClient client = new WebClient();
Console.WriteLine(message);
}
}
}");
The CSharpCompilation instance is created as below. I did not include the references for simplicity but it does add a reference to System.Net and few other common assemblies.
CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName,
syntaxTrees: new[] { syntaxTree },
references: references,
options: new CSharpCompilationOptions(
OutputKind.DynamicallyLinkedLibrary,
usings: new List<string> { "System", "System.Net" }));
The issue is that, if I comment out using System.Net, I get errors saying that:
CS0246: The type or namespace name 'WebClient' could not be found (are
you missing a using directive or an assembly reference?)
What could cause this? The options passed to CSharpCompilation has the usings property set to System and System.Net. Is this the normal behavior or am I missing something? Even though the usings property is specified, source text still needs to have the using statement? I am pretty sure I am missing something!
This is used for scripting, not normal file compilations.
If you trace usages of that property through Roslyn source, you'll end up here, which is used in the interactive pipeline (that's what a Submission is).

Object in ProjectReference not accessible when using 'Import'

In a simple project i want to reference objects from a webreference added to another c# library.
The Webreference is called QServices The default namespace is set as below
What seems to work is the following code:
Taskworkflow.SI.QServices.Record[] querysResult = new Taskworkflow.SI.QServices.Record[0];
yet when i import the Taskworkflow.SI - namespace, i keep on getting errors:
using TaskWorkflow.SI;
....
QServices.Record[] querysResult = new QServices.Record[0];
This results in the error:
The type or namespace name 'QServices' could not be found (are you missing a using directive or an assembly reference?)
Could someone clarify this for me?
Thank you for your time.
Note: QServices do only exist inside TaskWorkflow.SI. They do not have any occurences in other projects nor do they have any classes/namespaces/objects that share the name.
I strongly suspect that for whatever reason, you're ending up with a namespace called QServices declared in the TaskWorkflow.SI namespace. So actually you want:
using TaskWorkflow.SI.QServices;
....
Record[] querysResult = new Record[0];
Or you could explicitly alias it:
using QServices = TaskWorkflow.SI.QServices;
....
QServices.Record[] querysResult = new QServices.Record[0];

QuickGraph, how to use extension method StronglyConnectedComponents

As part of my first experiments with C# (on Mono 2.6.7) , I am trying to use the StronglyConnectedComponents method from QuickGraph. Here is my code:
using System;
using QuickGraph;
using QuickGraph.Data;
using System.Collections.Generic;
using QuickGraph.Algorithms;
namespace Graph
{
class MainClass
{
public static void Main (string[] args)
{
IVertexListGraph<int,Edge<int>> graph;
graph = new AdjacencyGraph<int,Edge<int>>();
IDictionary<int,int> components=new Dictionary<int,int>();
int noc = graph.StronglyConnectedComponents(out components);
}
}
}
When trying to compile the code above, I get the error message (in MonoDevelop):
Error CS1061: Type `QuickGraph.IVertexListGraph<int,QuickGraph.Edge<int>>' does not
contain a definition for `StronglyConnectedComponents' and no extension method
`StronglyConnectedComponents' of type
`QuickGraph.IVertexListGraph<int,QuickGraph.Edge<int>>' could be found
(are you missing a using directive or an assembly reference?) (CS1061) (Graph)
As for as I can see from the documentation, the extension method should be available:
public static int StronglyConnectedComponents<TVertex, TEdge>(
IVertexListGraph<TVertex, TEdge> g,
out IDictionary<TVertex, int> components
)
Also, I referred all three .dll's from QuickGraph. What am I missing?
Ok, I just checked it out and it works for me on Mono 2.10.5 (Ubuntu), which I currently have, so consider updating. 2.6.7 is very old. I just downloaded quick graph library, referenced only one dll (QuickGraph.dll), copypasted your code (just removed using QuickGraph.Data) and it compiles and runs without any problem.

Categories