System.Linq.Queryable is not an attribute class error? - c#

I am getting an error when adding [Queryable] the error is similar like this 'System.Linq.Queryable' is not an attribute class
when I tried to add namespace before [System.Web.Http.Queryable] I got another error
The type System.Web.Http.QueryableAttribute exists in both
System.Web.Http.OData.dll and System.Web.Http.dll
How to resolve this?

As Soner Gönül said.
Most probably you have
two using directives in your cs file:
using System.Web;
using System.Web.Http;
both define the attribute. Either remove one of those namespaces or fully qualify your attribute either as
[System.Web.Queryable]
// or
[System.Web.Http.Queryable]
Depending what the difference is of both and what you need. You will find more help in the corresponding MSDN documentation of both.

You have two DLL references that contain exactly the same namespace, containing a class with exactly the same name. The simplest solution is to remove one of these references from your project. If you need them both for some other reasons then the only solution I can think of is to create another project which will wrap the classes you need within other classes in a different namespace.

Related

Compilation error due to same attribute type in multiple dependencies

In my code I have two dependencies (here DEP_1 and DEP_2) that both define a SerializableAttribute. When I add [Serializable] to a class it complains that:
The type 'SerializableAttribute' exists in both 'DEP_1' and 'DEP_2'
Now I want to use the attribute from DEP_2, but even if I use [DEP_2.path.SerializableAttribute] and only have using DEP_2; it still complains about it.
Does anyone have a way around this, as I definitely need both dependencies in the project I'm trying to work with, or would I be best creating an entirely separate project and referencing it?
Do you know how to use namespace aliases? Or just fully-qualify the attribute name everywhere you use it: System.SerializableAttribute.

Xamarin\C# same namespace for all classes?

I have a Xamarin solution with 4 project inside, PCL, Android, IOS and UWP.
This is my PCL'structure:
I would like to know if any classes that I created must be on the same namespace es. SgatMobileV2, or in a different namespace es. SgatMobileV2.Behaviors, SgatMobileV2.Helpers, SgatMobileV2.Models etc..
There are differences between this two method?
If yes, what should be the best one?
Thank you!
Namespaces are a way to organize your code. You should be able to use any (or none) system that you want. Many people like to use namespaces that follow the folder hierarchy in their code (this is the Visual Studio default behavior) but it's not strictly necessary.
It is OK for them to be in different namespaces, however if you are referencing a class from a different namespace, you must add a using statement to the top of the referencing class
using SgatMobileV2.Behaviors;
or fully qualify the class name when using it:
SgatMobileV2.Behaviors.MyBehavior b = new SgatMobileV2.Behaviors.MyBehavior();

Namespace Shadowing Class Name

I have the following situation:
A compiled library with the namespace Library which contains class Feauture.
Now there is another library in development, one which intends to utilize the feature, and that has been dubbed Library.Feature. Finally there is a third library: Library.Feature.UI.
When working in the Library.Feature.UI project, which has both other libraries referenced, VS is yelling a lot about trying to using the Feature class, because it is seeing it primarily as a namespace.
I've tried a few different using directives to get around this, as well as trying to qualify the class name, but nothing is working.
Assuming I don't have the ability to change any of the namespaces or existing class names, is there a way to circumvent this issue?
You can either use fully qualified names where you specify the namespace together with the type name or you can use a using directive to create an alias:
using MyFeature = Library.Feature;
You can use alias directives to give a different name to any namespace, and then use that alias to reference that namespace.
By doing this you can differentiate between the class and the namespace.
write the following on top while using namespaces.
using FeatureClass = Library.Feature;
For more knowledge on this, you can refer:
https://msdn.microsoft.com/en-us/library/aa664765%28v=vs.71%29.aspx
Let me know if you have any further issue...
This should do the trick (references)
using FeatureClass = Library.Feature;

C# Using Directive - Including All Child Libraries [duplicate]

Sorry if this question was asked already.
I started studying C# and noticed that C# doesn't automatically import nested namespaces.
I don't understand:
using System;
should automatically import all classes contained in the System namespace right?
So there should be no need for me to write
using System.Windows.Form;
I would understand if using Windows.Form even worked. But the compiler could not resolve it! What is the point of the using System; before it then?
So why does using System; not import System.Windows automatically as well as System.Windows.Forms - sorry if the word import is wrong here.. maybe move to global namespace is the right terminology.
C# is not Java.
A using directive is used so you don't have to type in the fully qualified name of a type. It also helps with disambiguating type names (using aliases for instance).
In the case of Console, for example, you don't need to type System.Console.
It is important to understand the difference between a namespace and an assembly - a namespace is a logical grouping of types. An assembly is a physical grouping of types. Namespaces can span assemblies.
When you reference an assembly (this is more like importing a package in Java), you gain access to all of the public types in it. In order to use a type you need to uniquely identify it. This is done through the namespace - the using directive simply means you don't have to type the fully qualified name of the type.
The using directive has two uses:
To allow the use of types in a namespace so that you do not have to
qualify the use of a type in that namespace:
using System.Text;
To create an alias for a namespace or a type. This
is called a using alias directive.
using Project = PC.MyCompany.Project;
http://msdn.microsoft.com/en-us/library/sf0df423.aspx
However, you have to note that System and System.Windows.Form are not connected through name itself in anyway. If you import (using) System that means you will use the System assembly types in this class. Actual reference you specify in references section in Visual Studio project which you can really use (even without using statement, as this is just a shortcut for types).
C# doesn't import nested namespaces and this is by design.
Namespace scope lets you organize code and gives you a way to create
globally unique types.
Nested namespaces are used to group related functionality, but use parts of it on-demand.
I guess you wouldn't want to have all the types from such a big namespace like System if the only thing you need is System.Windows.
So probably the question is why C# doesn't have something like using System.*; like java does. I don't know the answer, but I guess this is because of KISS principle. It's something like using
select *
you will never know what types you will add and how they will affect existing code.
Even in Java you'd have to explicitly write
import System.*;
Much of the time you don't want all of the nested namespaces. These would simply clutter IntelliSense.
The "using" syntax allows you shorthand access to namespaces that are already listed as References in the project settings. If the namespace is listed as a reference you already have access to it by it's full name without the "using" directive. Just saves keystrokes.
"Using" a given namespace means that you will get access to all definitions implemented directly in it, not that it will recursively look up the embedded namespaces; doing otherwise would defeat the purpose of the "Using" statement.
Namespaces exist to avoid class name ambiguity. The "Using" statement is here to avoid the use of fully qualified types nested in namespaces, when you know no (or little) ambiguity may occur.
No, this is not how it works.
And I will give a good argument against what you said: intellisnse would go crazy and finding the what you want would be hell.
You do have access to everything on every namespace available (with dots), the using keyword simplifies this because you don't have to specify from which namespace a class or struct is "coming from" (I mean, defined).

exclude a class from a used namespace

The first statement of all my C# files is "using System;".
Now with framework version 4 this namespace contains a class called "Action". This is also the name for a class im my own code in a regularly used namespace. Now there is of course a conflict. Ofcourse I can resolve this conflict by using explicit "MyNamespace.Action" where ever I was using "Action" before. This are several hundreds if not thousands of lines. Or I could not use the System namespace, which of course leads to many other problems. I would like to write something like "using System but not System.Action", but I cannot find a statement to do this.
Any ideas?
No, you can't.
But you can add using Action = MyNamespace.Action. This will be highly confusing for new developers, though, as Action is a fundamental part of .net since 3.5 so I strongly suggest you rename your class.
The using directive has two uses:
To permit the use of types in a namespace so you do not have to qualify the use of a type in that namespace:
using System.Text;
To create an alias for a namespace or a type (you could go for this one).
using Project = PC.MyCompany.Project;
The using keyword can also be used to create using statements, which define when an object will be disposed. See using statement for more information.
using directive (C# Reference)

Categories