Conflits between namespace and library - c#

I'm using a namespace ListExtensionsMethods(defined in file ext.cs) both in my C# dl.dll and in the main.cs file. But when I try to include dl.dll in the main.cs I get about 30 error messages like this:
Warning 2 The type 'ListExtensionsMethods.MachineExtensions' in
'b:\library\C#\ext.cs' conflicts with the imported type
'ListExtensionsMethods.MachineExtensions' in 'C:\path\update.dll'.
Using the type defined in
'b:\library\C#\ext.cs'. C:\path\db.cs 52 39 testing
How do I fix this?

I believe you should be able to use the global namespace, however this warning exists for a reason and you should make sure you really want to use the same namespacing.

Related

I cannot reference Microsoft.Owin.Security.AuthenticationManager, why?

I get the following error on my page:
Server Error in '/' Application.
The current type, Microsoft.Owin.Security.IAuthenticationManager, is an interface and cannot be constructed. Are you missing a type mapping?
I founded out that I should put this code in Unityconfig.cs to resolve this problem:
container.RegisterType<IAuthenticationManager>(
new InjectionFactory(
o => System.Web.HttpContext.Current.GetOwinContext().Authentication
)
);
But the problem is that the IAuthenticationManager is not visible
although I have added the Owin.Security as reference, I have the
using Microsoft.Owin.Security;
Can you please give me some hints?
Note that the interface you are trying to use (Microsoft.Owin.Security.IAuthenticationManager) isn't in Microsoft.Owin.Security.dll but is actually in Microsoft.Owin.dll. Check the two lines at the top that tell you both the namespace and the assembly. So you just need to add a reference to that assembly.
For situations like this, it's always worth checking the docs as the namespace doesn't always equate to the assembly name.

Error "CS0104: 'DataType' is an ambiguous reference between 'System.ComponentModel.DataAnnotations.DataType' and 'CarlosAg.ExcelXmlWriter.DataType"

I am getting this error:
"CS0104: 'DataType' is an ambiguous reference between
'System.ComponentModel.DataAnnotations.DataType' and
'CarlosAg.ExcelXmlWriter.DataType'"
while running an ASP.NET 4.0 application. Can any one help me on this issue?
As described in the documentation on Compiler Error CS0104 you've got a symbol clash - there are two classes in scope of your source file which are both called DataType - one is in the namespace System.ComponentModel.DataAnnotations and the other is in the namespace CarlosAg.ExcelXmlWriter.DataType'.
You need to do one of the following to resolve this:
1. Explicitly provide the full namespace prefix on each usage, i.e.
System.ComponentModel.DataAnnotations.DataType and CarlosAg.ExcelXmlWriter.DataType:
var cdt = new CarlosAg.ExcelXmlWriter.DataType();
var sdt = new System.ComponentModel.DataAnnotations.DataType();
OR 2. Or use a using directive to alias the namespaces / types, e.g.
using SystemDT = System.ComponentModel.DataAnnotations;
using Carlos = CarlosAg.ExcelXmlWriter;
And then then identify the types with the namespace aliases, e.g..
var dt = new Carlos.DataType();
OR 3. You can also alias at the class level:
using SystemDataType = System.ComponentModel.DataAnnotations.DataType;
using CarlosDataType = CarlosAg.ExcelXmlWriter.DataType;
...
var myObj = new CarlosDataType();
OR 4. If you don't need to symbols from both namespaces, then delete the unused namespace from the using clause.
My preference would be for Option 2 - it makes it clearer to the reader that there is a namespace clash, without being too verbose (like Option 1 is)
Edit
Re: "I tried by giving full prefix but still I am getting error "CS0138: A
using namespace directive can only be applied to namespaces;
'CarlosAg.ExcelXmlWriter.DataType' is a type not a namespace"
(All relating to point #2, above). The error message refers to a situation like this, which isn't permitted in .Net (but is permitted in Java imports)
// i.e. This won't work, can't import at a class level unless it is aliased
using System.ComponentModel.DataAnnotations.DataType;
As per my answer, I would recommend that you alias the namespace, and then use the alias prefix to disambiguate between the 2 DataTypes
I had the same problem. The solution I used was to delete all using and make the references again. Worked perfectly.

Why can't I use the following IEnumerable<string>?

I'm getting the following error:
Error 25 The type or namespace name 'IEnumerable' could not be found (are you missing a using directive or an assembly reference?) C:\Development\Leverage\Leverage\Reports\SurveyLevel.aspx.cs 39 17 Leverage
because of this line:
private IEnumerable<string> GetDateParameters()
How do I deal with this? I tried to add in the line:
using System.IDisposable
at the top, but this doesn't fix it.
As others have said, you're missing using System.Collections.Generic;.
But that's giving you a fish; we should be teaching you to catch your own fish.
The way to solve this problem on your own is:
Enter the name of the type into your favourite search engine, and see what comes back:
IEnumerable(T) Interface (System.Collections.Generic)
http://msdn.microsoft.com/en-us/library/9eekhta0
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
See the bit that I highlighted in bold there? That's the namespace that you're missing.
If you still get the error then you are likely missing a reference; you can find out which DLL you have failed to reference by clicking on the link and reading the documentation page; it will tell you which DLL to reference.
You are missing a using System.Collections.Generic; statement at the top of the code file.
The generic IEnumerable<T> type cannot be found directly.
You could declare the full name instead:
private System.Collections.Generic.IEnumerable<string> GetDateParameters()
IEnumerable is in System.Collections
IEnumerable<T> is in System.Collections.Generic
You just need to add System.Collections.Generic namespace top of your code.
IEnumerable<T> belongs on this namespace in mscorlib.dll assembly.
You can use it like;
private System.Collections.Generic.IEnumerable<string> GetDateParameters()
Above answers are good. In my case, even after following the above answers it did not resolve the issue. Still the red squiggly constantly appeared.
Issue was the Framework of the project. It was by default set to .NET Framework 4.0.3 and changing to .NET Framework 4.0.0 will also help.
Save your Project properties after the change, build and it should all work.
Hope this helps.

Resolving an ambiguous reference

I'm trying to create a manager class to use with my charting tool, the problem is the tool I use, uses the same names for both a 3d and 2d charts which is resulting in ambiguous reference when I try to add the 2d library.. any ideas how best to resolve this?
For example,
using tool.2dChartLib;
using tool.3dChartLib;
BorderStyle is a member of both of these
I've tried casting the areas where I use BorderStyle. I suppose it could work if i just reference tool but then that would mean I'd have hundreds of tool.class lines instead of class
If the types with the same name exist in both namespaces, you have a couple of options:
1) If the number of types is small, create an alias for that type:
using BorderStyle3d = tool.3dChartLib.BorderStyle;
2) If the number of types is large, you can create an alias for the namespace:
using t3d = tool.3dChartLib;
Then in your code...
t3d.BorderStyle
You can use full type names, or create aliases:
using 2dBorderStyle = tool.2dChartLib.BorderStyle;
Use namespace alias
using twoDimensionLib = tool.2dChartLib;
using threeDimensionLib tool.3dChartLib;
I had similar problem that the class had ambiguous reference for the SAME namespace, so I deleted a specific Project (under Dependencies/{my.prj.name}.API) which had duplicated reference.
After that I referenced Project back with using of CTRL+.
Hope it'll work for you.
New reference of specific class

Alias and namespace conflict in Visual Studio Designer

I have a namespace conflict between two referenced assemblies:
i.e., I'm referencing Foo.A.Foo and Foo.Bar, so when I say I want Foo.Bar.Control, VS is trying to find Foo.A.Foo.Bar.Control
I can twiddle the Designer.cs code by adding new global:Foo.Bar.Control(), but as soon as I change anything, VS switches back.
I know there's something about adding aliases directly to the reference, I've tried but haven't managed to find the right combination (inline alias, using alias, reference alias).
Help?
"extern alias" may be what you mean, but I'm not sure what the designer will do with it, unfortunately...
I'm not even sure that's what you're after though - that's normally for two types from different assemblies with the same name.
You can write namespace aliases with a using directive, e.g.
using FooControl = Foo.Bar.Control;
but again, the designer is going to rewrite your code...
OK, this isn't the answer, but it's what I found for a workaround:
namespace FooBar
{
class FooBarControlHack : Foo.Bar.Control { }
}
So I can do the following in the Designer.cs :
this.fooBarControl = new FooBar.FoorBarControlHack();

Categories