Visual Studio 2015 intellisense and ambiguous namespaces - c#

The Xamarin.Auth project broke up i.e. the source code was compiling OK and after another Xamarin.Android update it was suddenly broken.
It turned out that the problem arose after the Mono.Android.dll (Xamarin.Android) was extended by several namespace definitions.
I hunted the problem down to the following:
There is a dll #1 (Mono.Android.dll):
namespace Xamarin.Android
{
public class Class1
{
}
}
namespace Android.OS
{
public class Class2
{
}
}
There is a dll #2 (Xamarin.Auth.Android.dll):
namespace Xamarin.Auth
{
//This does not compile. See the problem description below.
public class User1 : Android.OS.Class2
{
}
}
Intellisense shows the following problem:
Error CS0234 The type or namespace name 'OS' does not exist in the namespace 'Xamarin.Android' (are you missing an assembly reference?)
This can be fixed by changing the latter namespace to something else or by using the global:: identifier:
namespace SomeOtherNamespace
{
//This compiles ok.
public class User1 : Android.OS.Class2
{
}
}
namespace Xamarin.Auth
{
//This compiles ok.
public class User1 : global::Android.OS.Class2
{
}
}
The question is: why does not intellisense give a warning that the Android namespace branch is ambiguous between global::Xamarin.Android and global::Android? What is the good way out of it? Always use global:: namespace identifier?

You can use the global directive to tell the compiler it should evaluate the namespace from the root, else it tries to evaluate the namespace relative from the current namespace. Using global always uses the full qualified namespace name, so that is why it works when you prefix it.
Another option would be using an alias:
using AOS = Android.OS;
namespace Xamarin
{
public class User1 : AOS.Class2
{
}
}

Related

Folder structure unmasks [erroneous?] CS0246: "The type or namespace name 'IAsyncRepository<>' could not be found"

RE: DotNet 6.0 and VS Community 2022 (64-bit) - CurrentVersion 17.1.1
This code results in a compiler error CS0246:
using GloboTicket.TicketManagement.Domain.Entities;
namespace GloboTicket.TicketManagement.Application.Contracts.Persistence
{
public interface IOrderRepository: IAsyncRepository<Order>
{
}
}
This following code does not (note the use of the prefix "Persistence." to refer to IAsyncRepository):
using GloboTicket.TicketManagement.Domain.Entities;
namespace GloboTicket.TicketManagement.Application.Contracts.Persistence
{
public interface IOrderRepository: Persistence.IAsyncRepository<Order>
{
}
}
Why is this needed? IAsyncRepository and IOrderRepository are siblings in the same folder - i.e., in the same namespace!
The reference can be used without the "Persistance." prefix if the "where T : class" is deleted in the base type "IAsyncRepository":
namespace GloboTicket.TicketManagement.Application.Contracts.Persistence
{
public interface IAsyncRepository<T> where T : class
{
/...
}
}

MEF - Export Module not loaded

Following MEF Export will not be loaded by ComposeParts(this).
using MyLib;
using System.Composition;
namespace Test
{
[Export(typeof(IExtension))]
public class Test : IExtension
{
}
}
This happens when you auto-install MEF to dotnetcore using Visual Studio Alt+Enter menu. The correct namespace is System.ComponentModel.Composition. Due to quite similarly named namespace this may be overseen.
using MyLib;
using System.ComponentModel.Composition; // <--
namespace Test
{
[Export(typeof(IExtension))]
public class Test : IExtension
{
}
}

Referencing of classes and namespaces Visual Studio 2017

I noticed for my little project that when importing classes some use full folder reference while otheres don't.
Here is code from project Mini that i am working on.
Models folder
Contains two entities, Auto and Airplane
namespace Mini.Models {
public class Auto {
// code and stuff
}
}
namespace Mini.Models {
public class Airplane {
// code and stuff
}
}
Services folder Contains single service class
namespace Mini.Services
{
public class AutoService : IAutoService {
public bool Get() {
var autoObject = new Models.Auto(); // notice how it references Models folder
var planeObject = new Airplane(); // Same folder but not referencing Models in front of it
// other code
}
}
public interface IAutoService {
bool Get();
// others
}
}
While not a major bugbear, it is still annoying that two classes in same folder get referenced differently, and i cannot figure out why.
Any advice would be appreciated.
Error Message when removing Models folder
Error CS0118: 'Auto' is a namespace but is used like a type (34, 27)
Based on the error message you have provided:
Error CS0118: 'Auto' is a namespace but is used like a type (34, 27)
It would appear that you have a namespace called Auto. Imagine the following example:
namespace MyApp.Auto
{
class Test
{
}
}
namespace MyApp
{
class Auto
{
}
class MyTest
{
private Auto test;
}
}
Because you can see, from the MyApp namespace, both a class called Auto and a namespace called Auto (either namespace MyApp.Auto or simply namespace Auto), C# isn't sure which one you want. As such, it's forcing you to be specific in choosing one or the other.
The easiest solution is to change the MyApp.Auto namespace to something else.
This is not fix but explaining with proper code sample (and why ).
namespace Mini.Models
{
public class Auto
{
// code and stuff
}
}
namespace Mini.Models
{
public class Airplane
{
// code and stuff
}
}
namespace Mini.Auto
{
public class OtherAirplane
{
// code and stuff
}
}
namespace Mini
{
using Mini.Models;
using namespaceAuto = Auto ; /// this also not fix the issue.
class NamespaceIssue
{
void execute()
{
var autoObject = new Auto(); // Error
var planeObject = new Airplane(); // Same folder but not referencing Models in front of it
// other code
}
}
}
now you can see some were in code you have "Mini.Auto" namespace , and it is couching issue.
i tested for VS 2015 have same issue. maybe we have to report to VS team or it is by design .
The issue seemed to be with VS2017 or the way it created the project first time around.
Upon starting brand new project (ASP Core 2.2, Web API, with https enabled and docker disabled), and using same classes the issue was non-existant.

web service error CS0246: The type or namespace name could not be found VS2010

I've added both a Web Service and a Web Reference to my project and can consume neither (one of which I named emsBookings) appropriately because I get the following error:
Compiler Error Message: CS0246: The type or namespace name 'emsBookings' could not be found (are you missing a using directive or an assembly reference?)
I tried using:
using emsBookings;
But that did not resolve the issue - and instead the error pointed to that line when I added that code.
My code looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
public string xmlRequest;
protected void Page_Load(object sender, EventArgs e)
{
WebApplication1.emsBookings.GetAllBookingsRequestBody client = new WebApplication1.emsBookings.GetAllBookingsRequestBody();
//inputs required for request
string username = "theusername";
string password = "thepassword";
DateTime startDate = System.DateTime.Today;
DateTime endDate = System.DateTime.Today;
int buildingID = 36;
bool viewCombo = false;
client.UserName = username;
client.Password = password;
client.StartDate = startDate;
client.EndDate = endDate;
client.BuildingID = buildingID;
client.ViewComboRoomComponents = viewCombo;
emsBookings.GetAllBookingsRequest request = new emsBookings.GetAllBookingsRequest();
request.Body = client;
xmlRequest = request.Body.ToString();
}
}
}
Is there a specific way I need to reference the web-reference and/or web-service in order to use it in my code?
In Web.Config I find reference to emsBookings in the following:
<configuration>
//Extra stuff removed for brevity
<appSettings>
<add key="emsBookings.Service" value="https://my.fully.qualified.server/THEAPI/Service.asmx"/>
I've tried:
rebuilding (no success)
cleaning / rebuilding (no success)
adding 'using emsBookings;' (no success)
Any other ideas on what to try?
I'm fairly limited in my access to the server, so digging through logs in the root C: drive or things like that I would like to try to avoid if possible.
This is not a duplicate as far as I can tell as I am targeting .NET 3.5 and in the Property Pages, the build Target Framework is .NET Framework 3.5; I cannot find any reference to a Client Profile anywhere which was the indicated issue in the other 'possible duplicate' question.
Here is a link to the VS2010 showing the service reference added to my application: http://imgur.com/hIOCqKJ
Here is a link showing that the service reference exists in the same namespace (object browser): http://imgur.com/GcJ133p
Here is some of the Reference.cs:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.18444
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace WebApplication1.emsBookings {
using System.Runtime.Serialization;
using System;
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.CollectionDataContractAttribute(Name="ArrayOfInt", Namespace="http://DEA.EMS.API.Web.Service/", ItemName="int")]
[System.SerializableAttribute()]
public class ArrayOfInt : System.Collections.Generic.List<int> {
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.CollectionDataContractAttribute(Name="ArrayOfString", Namespace="http://DEA.EMS.API.Web.Service/", ItemName="string")]
[System.SerializableAttribute()]
public class ArrayOfString : System.Collections.Generic.List<string> {
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://DEA.EMS.API.Web.Service/", ConfigurationName="emsBookings.ServiceSoap")]
public interface ServiceSoap {
// CODEGEN: Generating message contract since element name GetAPIVersionResult from namespace http://DEA.EMS.API.Web.Service/ is not marked nillable
[System.ServiceModel.OperationContractAttribute(Action="http://DEA.EMS.API.Web.Service/GetAPIVersion", ReplyAction="*")]
WebApplication1.emsBookings.GetAPIVersionResponse GetAPIVersion(WebApplication1.emsBookings.GetAPIVersionRequest request);
I noticed from your screenshot that you have created a website and that is referenced in the solution?
If you have, its better to create an ASP.NET Web Application and create the service reference again, as from what you have explained.
I have performed my own suggestion the reference has worked as expected.

How to compile C# application through Command Prompt

I have one folder. In that I have three cs files.
Demo.cs
using System;
namespace Demo
{
public class Test
{
public static Entity entity = new Entity();
public static void Main(string[] args)
{
var objectHandler = Activator.CreateInstance(null,
args);
var obj = objectHandler.Unwrap();
entity.GetAnnotation(obj.GetType());
}
}
}
Entity.cs
using System;
namespace Demo
{
public class Entity
{
public void GetAnnotation(Type classname)
{
Attribute[] dataAnnotationlist = Attribute.GetCustomAttributes(propInfo);
foreach (var dataannotationAttribute in dataAnnotationlist)
{
//some operation to get annotation property from Employee.cs class
}
}
}
}
Employee.cs
using System.ComponentModel.DataAnnotations;
namespace Demo
{
public class Employee
{
[Display(Name = "name")]
public string name { get; set; }
}
}
I have created XML file format from class file (Employee.cs) using reflection.
But error occurred when try to run through Command Prompt. It runs in visual studio.
I want to run Test.cs, Entity.cs using command prompt with passing "Employee.cs" as a string parameter to Main method.
Now, I have passed hard coded as,
System.Runtime.Remoting.ObjectHandle objectHandler = Activator.CreateInstance(null, "Demo.Employee");
Its working fine but how to pass it through command.
Error Occurred is:
Entity.cs(8,29): error CS0234: The type or namespace name
'DataAnnotations' does not exist in the namespace
'System.ComponentModel' (are you missing an assembly reference?) Entity.cs(9,19): error CS0234: The type or namespace name
'Objects'
does not exist in the namespace 'System.Data' (are you missing an
assembly reference?) Employee.cs(6,33): error CS0234: The type or namespace name 'DataAnnotations' does
not exist in the namespace 'System.ComponentModel' (are you missing an
assembly reference?)
and it also shows error for "DataAnnotations" and "Objects".
how can i solve this problem?
One option to simply build .csproj with MSBUILD.
More entertaining is to configure all dependencies yourself via command line arguments of csc. For your immediate error you need to add references with /r: command similar to following
csc /out:Test.exe /r:System.ComponentModel.DataAnnotations.dll *.cs
For more details on command line arguments of csc check help csc /? or on MSDN CSC command line options and Building with CSC.

Categories

Resources