Assembly access - c#

If you have a assembly identity/namespace of Library.Testing.
Then you created another assembly with identity/namespace of Library.Testing.One
Library.Testing.One project references Library.Testing.
Why is it you have to use using Library.Testing; in your classes in Library.Testing.One to access anything in Library.Testing?
Example1:
using System;
namespace Library.Testing.One
{
// 'Library.Testing' is a reference in this assembly
public class foo : Library.Testing.BooBase
{
}
}
This does not work I get two exception
Warning 1 Load of property
'RootNamespace' failed. The string
for the root namespace must be a valid
identifier. Error 2 The type or
namespace name 'BooBase' does not
exist in the namespace
'Library.Testing.One.Library.Testing'
(are you missing an assembly
reference?)
Example2:
using System;
using Library.Testing;
namespace Library.Testing.One
{
// 'Library.Testing' is a reference in this assembly
public class foo : Library.Testing.BooBase
{
}
}
This works!

Adding a "using" for Library.Testing.One does not automatically bring everything in Library and Library.Testing into scope. The fact that the namespaces appear to be hierarchical is probably what's leading to your confusion.
Think of, for example, adding using System.Data.SqlClient to a file. That doesn't automatically bring everything in System and System.Data into scope.

Related

Can't find linked class

I have two projects in the same solution and I have linked a class DatabaseFunctions.cs to the second one.
The problem is that when I reference it in the GUI_Teacher.cs I get the error:
Error CS0246 The type or namespace name 'DatabaseFunctions' could not be found (are you missing a using directive or an assembly reference?) Teachers application C:\Users\user1\source\repos\IASLSN\Teachers application\GUI_Teacher.cs
From the error, you are simply missing a reference.
Take the following steps to add a reference
Right click on the GUI_Teacher.cs project and click on Add Reference
From the left side of the Add Reference dialog, click on Solution
Select DatabaseFunctions.cs from the list and click OK to close the dialog.
In the source file DatabaseFunctions.cs you have something like
using System;
namespace SomeNamespaceName
{
public class DatabaseFunctions
{
}
}
To use the class in your GUI_Teacher.cs source file you need to add either using SomeNamespaceName; at the top or use the fully qualified type name SomeNamespaceName.DatabaseFunctions instead.
Keep in mind that the name of a sourcecode file like your DatabaseFunctions.cs for example has no connection to the name of a class or a namespace in that file. You can put a class like public class DatabaseFunctions { } in a file called foo.cs. And in the using directive at the top of your sourcefiles you provide a namespace and not a filename.

Namespace not found error when compiling assembly from files

What I'm trying to achieve is generate a project dynamically from c# classes generated by me.
This classes' content are a similar content of code-first code generation of entity framework.The content looks as follows:
namespace ElasticTables
{
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations.KeyAttribute;
[Table("address")]
public partial class address
{
[Key]
public decimal id { get; set; }
public string name { get; set; }
}
}
I generate this files from the tables in my database, and then try to compile it programmatically so that I can reference the generated project in another project that works with an API.
The main errors while compiling is:
The type or namespace name 'KeyAttribute' does not exist in the namespace 'System.ComponentModel.DataAnnotations' (are you missing an assembly reference?)
The type or namespace 'Key' could not be found
The type or namespace 'Table' could not be found.
I'm using 'CSharpCodeProvider'
var provider = new CSharpCodeProvider();
var options = new CompilerParameters
{
OutputAssembly = "ElasticTables.dll",
CompilerOptions = "/optimize"
};
And I have the following referenced Assemblies
options.ReferencedAssemblies.Add(Directory.GetCurrentDirectory() + "\\EntityFramework.dll");
options.ReferencedAssemblies.Add(Directory.GetCurrentDirectory() + "\\EntityFramework.SqlServer.dll");
I have an string array with the files' paths called sources, and I try to compile with the following line
CompilerResults results = provider.CompileAssemblyFromFile(options, sources);
Help is much appreciated.
You need to reference all needed assemblies (as the error says), so you need to add, I'd say at the very least:
options.ReferencedAssemblies.Add("System.dll");
options.ReferencedAssemblies.Add("System.ComponentModel.DataAnnotations.dll");
Others might be needed
About the comment in your question, yes, you should specify options.OutputAssembly
Also, in your generated code:
using System.ComponentModel.DataAnnotations.KeyAttribute;
KeyAttribute is not a namespace, so it'll probably give an error when compiling.
I'd also take the usings before the namespace. This is not strictly needed and not an error, but it's the common practice (and that way you are sure the referenced assemblies are from the global namespace, and not childs of the namespace your class is in [just in case there's a name conflict])
Have you tried to add also a reference to "System.dll" and "System.ComponentModel.DataAnnotations.dll" (for the System.ComponentModel stuff) ?
(as you may indeed be missing an assembly reference)
options.ReferencedAssemblies.Add(
Path.Combine(
Directory.GetCurrentDirectory(),
"System.ComponentModel.DataAnnotations.dll"));

Error during Adding Class Library in My Project

I had created one Class library as BLL.Inside that one Class as ClsEmployeeMaster.
I want to use this library in my projet of Visitor Management.
when i was trying to add this as reff. then it added successfully but when I was start to Build then errors occurs as :
Error 5 The type or namespace name 'ClsEmployeeMaster' could not be
found (are you missing a using directive or an assembly reference?)
so What to do??
It is not enough to add a reference to your BLL. If you want to use the classes defined in your BLL you need to name them with the full namespace. Something like
MyCompany.MyBLL.ClsEmployeeMaster em = new MyCompany.MyBLL.ClsEmployeeMaster();
or you can simplify your typing with
using MyCompany.MyBLL;
....
ClsEmployeeMaster em = new ClsEmployeeMaster();
Open the class file that you have created.
Check the namespace written above the class.
for example
namespace MyCompany
{
class BLL
{
...
...
}
}
then add that namespace to your current .cs
using dllName.MyCompany;

Is there a way to reference a certain class/interface/... by enclosing it with its namespace rather than a using directive "using namespace_name"?

Is there a way to reference a certain class/interface/... by enclosing it with its namespace rather than a using directive "using namespace_name" ?!
As, I'm working on a website, which uses SAP .NET connector. I already added a reference for connector ddl, and while referencing its namespace "using namespace_name", or set class namespace to another one rather than connector namespace,
I got error regarding connector classes with that error message "The type or namespace couldn't be found, are you missing a using directive or an assembly reference?".
But while changing namespace name to connector namespace, everything is going well?!
// Set namespace to be IDestinationConfiguration interface namespace.
// Using this, everything is going well.
namespace SAP.Middleware.Connector
{
public class ConnectorConfiguration : IDestinationConfiguration
{
}
}
// Using that way; it's not working, and got an error regarding IDestinationConfiguration even it belongs to refernced namespace.
using SAP.Middleware.Connector;
public class ConnectorConfiguration : IDestinationConfiguration
{
}
So, connector types forced me to set namespace of class to their namespace!
Is this possible? If so, how?
I ran into this too and couldn't figure it out until I finally found the answer on a post at http://forums.sdn.sap.com/thread.jspa?threadID=1876430, the last line which said:
"Also, make sure your target framework is ".NET v4.0" NOT ".NET v4.0 Client" -- that way you get the System.Web namespace that is required."
My project was targeting 4.0 client target framework. Once I changed it to just .NET Freamework 4.0, all the references worked as expected.
Is this what you are after?
public class ConnectorConfiguration: SAP.Middleware.Connection.IDestinationConfiguration
{
}
You can write all your code without usings if you like, you just need to use the fully qualified namespace name for every class/interface where the using isn't used.
If you try this:
using SAPTEST = SAP.Middleware.Connection;
namespace TestNamespace
{
public class ConnectorConfiguration: SAPTEST.IDestinationConfiguration
{
}
}
If that works, but it doesn't work if you remove SAPTEST, then IDestinationConfiguration must be declared in another namespace too.

missing a using directive or an assembly reference?

I am getting this error Error 4 The type or namespace name 'Sample' could not be found (are you missing a using directive or an assembly reference?) F:\oadmin\sea\Controls\Dlg.cs 11 7 Controls
here i added Samplein reference section,,,then i used it as Using Sample,,i dont know why i am getting this error
this is the class
namespace sample
{
public class Server
{
public class client
{
Bool sent = true
}
}
}
Can i share exe,,this Sample is an exe
Perhaps because your namespace is sample and not Sample?
So if your Dlg namespace is different it needs to have a using sample line not using Sample.
Or perhaps your class is in another project within your solution and you need to include the hierarchy of namespaces?
Your namespace "sample" is lowercase, but you are referring to it in uppercase "Sample" ...
EDIT: You Bool sent is internal, it cannot be accessed from outside of the assembly

Categories

Resources