RootNamespace not being used as namespace prefix - c#

I recently created a new .net6 project and moved my source code into it.
I've set the following in the project file
<RootNamespace>$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
In the project properties there is a section at the bottom of Application -> General there is a "Default namespace" which also has "$(MSBuildProjectName.Replace(" ", "_"))"
For the sake of this question I have omitted the solution completely and let's assume my project structure is as follows
MyProject (this is a project with project file MyProject.csproj)
DB (this is a folder in the project)
MySQL (this is a sub-folder under the DB folder)
MSSQL (this is a sub-folder under the DB folder)
UI (this is a folder in the project)
If I add a new class at the root level of my project it's namespace is MyProject however if I add a new class to DB it's namespace is DB.
I expect this to be MyProject.DB
Similarly if I add a new class to the MSSQL project I get a namespace of
DB.MSSQL whereas I expect this to be MyProject.DB.MSSQL
I have tried hard coding the $(MSBuildProjectName.Replace(" ", "_")) as MyProject but this doesn't help.

As strange as this sounds, the problem was with WebView2 version 1.0.1343.22
Updating to version 1.0.1370.28 resolved the issue
I did also recreate this in a fresh solution with only 1 project and no other dependencies to ensure that is the case.
Interestingly the default using statements are also different.
With version 1.0.1343.22 installed, creating a new class file you get
using System;
using System.Collections.Generic;
using System.Text;
namespace DB.MsSQL
{
internal class Class1
{
}
}
Whereas with version 1.0.1370.28 a new class file looks like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebView2NamespaceIssue.DB.MsSQL
{
internal class Class2
{
}
}
Both of these classes were created in the same folder of the same project.
The only difference was changing the version of WebView2 being referenced

Related

Using AssembliesFromPath from StructureMap throws MissingMethodException

I am trying to scan all assemblies in a subfolder of my main project and then add the contained registry to my main registry in order to override default registry entries, where necessary and intercept types for dependency injection.
For this I create the following registry
public PluginRegistryAdder(string pluginPath)
{
Scan(x =>
{
x.AssembliesFromPath(pluginPath);
x.LookForRegistries();
});
}
which I plan to add to my main container like this:
var pluginRegistries = new PluginRegistryAdder(pluginPath);
Container.Configure(_ => _.IncludeRegistry(pluginRegistries));
The problem I am now facing is that I get this error, when using AssembliesFromPath and I don't know how to fix it:
System.MissingMethodException: Method not found: 'Void StructureMap.Graph.IAssemblyScanner.AssembliesFromPath(System.String)'.
Googling suggests that I should use the namespace StructureMap.Graph, but this did not solve the problem. Here are my using statements, of which only StructureMap is not greyed out (e.g. marked as not redundant) by Visual Studio (I was trying to find the missing reference, but nothing helped):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StructureMap;
using StructureMap.Graph;
using StructureMap.Configuration.DSL;
using StructureMap.Configuration;
using StructureMap.Graph.Scanning;
using StructureMap.Util;
Any suggestions, what I should try?!
UPDATES:
Note that when I comment out just the line with AssembliesFromPath, solution runs just fine, although LookForRegistries is definded in the same class as AssembliesFromPath.
AssembliesFromApplicationBaseDirectory gives me the same problem.
This problem seems to have been caused by a version mismatch between my DI project and my main project. After some trying I ended up reinstalling the newest version of StructureMap in both projects and the problem went away.

How do I designate the location of a library in order for a "using" statement to work (C#)

I'm used C# -some- but not recently.
I've got this header from some code, and it calls a custom non-system library, (Dynastream.Fit, at the bottom), but I need to know how to tell the program where it resides so it can compile. Pretty sure the library is in the SDK I downloaded, just need to find it. This is the FitSDK from ANT+.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using Dynastream.Fit;
You need to add the dll of Dynastream.Fit to your project Reference folder by right clicking the reference folder -> add reference -> Browse -> choose the dll; unless you have already installed the assembly in GAC.

Why class definition is changed when reference added to C# project?

I am working on C# on Win 7 Visual Studio 2012.
I have added a reference (defined and compiled in a .dll file in another location) to my C# project because I need to call a function that is defined in a class of that lib file.
But, when I check the definition from my current project, I find that the definition is changed and it is different from what I see when I open the source file where the class is defined.
I am new to C#.
What is the possible reason?
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Data;
using System.Diagnostics;
using System.Data.Odbc;
namespace MyName
{
public class MyDataLoad // the class definition is changed when I call it !!!
{
#region
// some functions definition
#endregion
void MyLoadDataFile()
{
}
}
}
UPDATE AGAIN
The definition of MyLoadDataFile() noes not exist in the .cs file where class MyDataLoad is defined. I open the file by right-clicking MyLoadDataFile() in my project.
The opened file is different from the source file that is used to generate the .dll lib, which is added as a reference to my project. But, I do not know how to solve the problem.
The definition of class MyDataLoad is different from what I see in the source file that is used to generate the .dll file. This is the reason why my c# program in my project cannot call MyLoadDataFile() because it is defined in the source file that is used to generate the .dll file. The definition of class MyDataLoad is different from the files that are used to get .dll file.
Thanks
I suspect that what you referring to is the fact that you can't see method MyLoadDataFile in your c# app. This is because you didn't use access modifier. You have
void MyLoadDataFile()
You need
public void MyLoadDataFile()
Your default modifier is private on class members. And this is why you don't see it

namespace or class could not be found (ASP.NET WebSite "project")

I am currently trying to cleanup a bit of a corporate website that I inherited here. I managed to clean up most of the errors in the website but something is still up here.
I have one masterpage that have this code :
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class MasterPage : System.Web.UI.MasterPage {
public lists m_listsClass = new lists();
(no it's not a typo the S in lists).
Now in App_code I have one class lists.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
/// Summary description for lists
/// </summary>
public class lists
{
public lists()
{
When I try to build the website in visual studio 2008 I have this error :
Error 3 The type or namespace name 'lists' could not be found (are you missing a using directive or an assembly reference?) C:\Users\egirard\Documents\Visual Studio 2008\Projects\iFuzioncorp\iFuzioncorp\Masters\MasterPage.master.cs 23 12 iFuzioncorp
Am I missing something ?
Also I saw some strange behaviour on the server. Apparently according to IIS7 it is compiling using .net 2.0 (the pool is configured with .net 2) but there are some "using" statements that include Linq ... how is it possible to compile the page if Linq is not part of the .net 2 itself ?
Just edited with news details on the code itself. No namespace at all everywhere.
Hi There – i had a similar problem; all my namespaces and inheritance was in place. Then i then noticed that the class file’s build action was set to “Content” not “Compile” (in the properties window.
For whatever worth might there be for an answer (possibly not the right one) after many months, i think i should contribute this:
There is a case that this happens, when you place a web site inside another (ie in a subfolder).
In that case, the only App_Code folder that is legitimate is the App_code folder of the outer web site. That is, the App_Code folder right under the root of the master web site.
Maybe (say maybe) there should be no need to transform your web site to a web application, if you place the class file inside the App_code folder of the ROOT web site.
include the namespace under which lists calss is defined
or
define both the master page and lists class under the same namespace
Finally I understood quite lately that it was a website and not a web application I had to question the guys here to get it... So it's quite normal all the error I had. I haven't had the occasion to convert it first.
Make sure that in your masterpage, you have an #include statement for the namespace that the lists class is a part of (if they're in seperate namespaces, the masterpage isn't going to automatically pick up on it).
As for the strange server side behavior, .NET 2.0, 3.0, and 3.5 all run inside of .NET 2.0 app pools in IIS. It looks strange at first, but you get used to it. Here's a link with a little more in-depth explination:
The Way I See It: Where is ASP.NET 3.5 on IIS?
In order to use a type, you must reference the assembly which defines it and include the appropriate namespace.
Using only includes namespaces, if you don't use any types from said namespaces it has no effect.
is lists.cs wrapped in a namespace? if it is the case then you need to add the namespace (yournamespace.lists) or include it in masterpage. Check Also if your MasterPage is in a Namespace
Modify the Build Action of the App_Code Class to Compile.
Clean and Build the Project
This worked for me.
I had this problem myself
It wasn't working because the project I was referencing was set as a console application in its properties instead of a class library

Referencing namespaces globally?

Is there a way to reference a namespace globally across the whole solution?
So instead of having these lines in every code file:
using System;
using MyNamespace;
having to declare them only once, and every code file would use them.
Btw I am using Visual Studio.
No, C# doesn't have this concept. Each source file is independent in this respect. (And if the using directives are in a namespace declaration, those are independent from other using directives in peer namespace declarations, too. That's a pretty rare case though in my experience.)
You don't need ReSharper to change what gets included in a new class though. You can use the Visual Studio templates.
EDIT: Just to clarify the point about using directives within namespaces, suppose we had (all in one file):
using Foo;
namespace X
{
using Bar;
// Foo and Bar are searched for code in here, but not Baz
}
namespace Y
{
using Baz;
// Foo and Baz are searched for code in here, but not Bar
}
Usually I only have one namespace declaration in a file, and put all the using directives before it.
No, this is not possible.
If you're using ReSharper, you can set an option to include specific using directives in every new file you create though.
From this SO question and follow-up blog post. You can edit the Visual Studio default templates.
To do this, look at the file in this zip : [Program Files][Visual Studio]\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
and modify the Class.cs file as needed. Additionally, Visual Studio may have cached this file here :
[Program Files][Visual Studio]\Common7\IDE\ItemTemplatesCache\CSharp\Code\1033\Class.zip
In C# 10.0 you can use Global Usings.
global using System;
global using MyNamespace;
No, you can not reference a namespace globally across the whole solution in .NET or .NET CORE.
But you can use project wise namespace globally in solution. this feature will be available from c#10/.NET 6. currently it's in preview but it will be released in NOV 2021
=========Project level .NET 6 global using namespace=========
Create a class file at root of the project e.g GlobalNamespace.cs
global using System;
global using System.Linq;
global using System.Text.RegularExpressions;
global using System.Threading.Tasks;
Then you don't need to declare using namespace in other .cs files of the project which are already declared globally.
As others have mentioned Visual Studio Templates are the way to go.
Note that simply adding a using statement to your template will not ensure that the compiler can resolve your types. So, if you are adding a using statement for MyNamespace in every class you may need to add an assembly reference to your project as well. See the C# FAQ for more information.
One trick I miss as a newb to CSharp is to look at the "refences" (in VS), to right click and "Add New Reference". This is especially handy when combining mulitple projects where I have made some generic class for reuse elsewhere.

Categories

Resources