简体   繁体   中英

How to use a DLL written in C# in other asp.net project?

I have a file named common.cs which contains commonly used functions like this

    public int GetColumnIndexByNameTemplateField(GridViewRow row, string SearchColumnName)
{
    int columnIndex = 0;
    foreach (DataControlFieldCell cell in row.Cells)
    {
        if (cell.ContainingField is TemplateField)
        {
            if (((TemplateField)cell.ContainingField).HeaderText.Equals(SearchColumnName))
            {
                break;
            }
        }
        columnIndex++;
    }
    return columnIndex;
}

Now I want to make that common.cs file change into common.dll, I created another class library project in which I copied all the code from common.cs file, added necessary references and built it to generate dll file, now I pasted that dll file from project debug directory into the bin directory of my asp.net project which is already using common.cs but whenever I try to import that dll using.

[DllImport("common.dll")]
public static extern String NewString(String x);

where NewString is another function, I get this error.

Unable to find an entry point named 'NewString' in DLL 'common.dll'.

I have checked already posted questions on stackoverflow as well as other guides on internet that tell how to make and use a dll but I don't know why I am getting this error also most of the guides show dll written in C++ where as my dll is written in C#, they also tell to use __declspec(dllexport) which tells that function is ready to be exported or something like that but I can't use this in C# dll I suppose.

I think its a basic question but this is my first time creating dll in C# so please help me out.

Thanks.

Ahmed if you want to turn any project into a library (.dll), double click the 'properties' folder in the solution explorer. Click the drop-down for project type and select 'Class Library' then click build, rebuild solution to recompile into a .dll.

Note that you can create a new ASP.NET project in the same solution by right clicking the solution and clicking 'add project'. Or you can open a new instance of Visual Studio. Once you open the ASP.NET project right click on references and click add reference. Click the browse tab, navigate to the folder containing the .dll and add it. Don't forget that any classes defined in the .dll must be marked as public or they will not be visible in other assemblies. Also don't forget the using statements for the namespaces that your classes are defined in.

When you click the 'Run Button' (the green arrow) it will always run the project that is in bold in the solution explorer. If a .dll is bolded then you will get an error that you cannot run a class library but you must instead change it to console application. If it is a console application then it must have a static void Main() function defined so it knows where to start. You can set any project as the startup project by right clicking it in solution explorer and click 'set as startup project'.

If you wish to run a program in your solution, but do not want to set it as the start up program than you can right click, go to debug and run. Organizing like-projects in a single solution can make maintaining them much easier.

Hope this helps.

You don't need to use DllImport . Since common.dll is a managed assembly, you simply need to add it as a reference in the ASP.NET project that's trying to use NewString .

if your assembly is written in c#, there's no need for a dllimport. rather go for add references like so - msdn entry (go for browse )

将dll粘贴到站点的Bin文件夹中,您可以通过在文件类顶部使用“使用”来使用dll,然后您将获得dll的所有方法。

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