简体   繁体   中英

cannot expose class in c#

This may seem like a beginner question, and it kind of is since this is my first large c# project conversion. Currently converting a large project from vb.net to c#, I understand c# syntax pretty well and the conversion is going pretty smoothly. My one question is - in vb.net I could expose a class with the import statement as such:

Imports DataAccessLayer.DataAccess

and say there is a method called GetDataTable in the DataAccess class, I can just call it by using GetDataTable(params)

Now, with C#, I can only do

Using DataAccessLayer;

and can't expose the DataAccess class, just the DataAccessLayer namespace and have to do DataAccess.GetDataTable(params), which will obviously add a lot of extra code to the project if I have to do that anywhere.

Is there any way to expose the class and not just the namespace? This is my constructor

namespace DataAccessLayer
{
    public class DataAccess
    {
    }
}

Thanks in advance!

If you are using C# v6 or above, you can import a static class like this:

using static Namespace.StaticClass;

So you can do the same for your class:

using static DataAccessLayer.DataAccess;

C#6支持如下

using static DataAccessLayer.DataAccess;

According to msdn here you can only access static members of a type without having to qualify the access with the type name like the following:

using static System.Console;

using static System.Math;
class Program 
{ 
    static void Main() 
    { 
        WriteLine(Sqrt(3*3 + 4*4)); 
    } 
}

You can create an alias for a type though:

using Project = PC.MyCompany.Project;

I can answer my own question, for anyone else that runs into this problem - i used

using static DataAccessLayer.DataAccess;

adding the static made it work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM