简体   繁体   English

如何在其他asp.net项目中使用C#编写的DLL?

[英]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 我有一个名为common.cs的文件,其中包含这样的常用功能

    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. 现在,我想将common.cs文件更改为common.dll,我创建了另一个类库项目,在其中我复制了common.cs文件中的所有代码,添加了必要的引用并生成了dll文件,现在我将其粘贴了从项目调试目录中的dll文件到我已经使用common.cs的asp.net项目的bin目录中,但是每当我尝试使用导入该dll时。

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

where NewString is another function, I get this error. 其中NewString是另一个函数,出现此错误。

Unable to find an entry point named 'NewString' in DLL 'common.dll'. 在DLL'common.dll'中找不到名为'NewString'的入口点。

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. 我已经检查了关于stackoverflow的已发布问题以及互联网上的其他指南,这些指南讲述了如何制作和使用dll,但我不知道为什么会收到此错误,而且大多数指南都显示了用C ++编写的dll,其中我的dll是用C#编写的,他们还告诉使用__declspec(dllexport),它告诉您函数可以随时导出或类似的操作,但我想我不能在C#dll中使用它。

I think its a basic question but this is my first time creating dll in C# so please help me out. 我认为这是一个基本问题,但这是我第一次使用C#创建dll,所以请帮帮我。

Thanks. 谢谢。

Ahmed if you want to turn any project into a library (.dll), double click the 'properties' folder in the solution explorer. 艾哈迈德(Ahmed)如果您要将任何项目转换为库(.dll),请在解决方案资源管理器中双击“属性”文件夹。 Click the drop-down for project type and select 'Class Library' then click build, rebuild solution to recompile into a .dll. 单击项目类型的下拉菜单,然后选择“类库”,然后单击“生成,重新生成解决方案以重新编译为.dll”。

Note that you can create a new ASP.NET project in the same solution by right clicking the solution and clicking 'add project'. 请注意,您可以在同一解决方案中创建一个新的ASP.NET项目,方法是右键单击该解决方案,然后单击“添加项目”。 Or you can open a new instance of Visual Studio. 或者,您可以打开Visual Studio的新实例。 Once you open the ASP.NET project right click on references and click add reference. 打开ASP.NET项目后,右键单击引用,然后单击添加引用。 Click the browse tab, navigate to the folder containing the .dll and add it. 单击浏览选项卡,导航到包含.dll的文件夹并添加它。 Don't forget that any classes defined in the .dll must be marked as public or they will not be visible in other assemblies. 不要忘了.dll中定义的任何类都必须标记为public,否则它们在其他程序集中将不可见。 Also don't forget the using statements for the namespaces that your classes are defined in. 另外,不要忘记定义类的名称空间的using语句。

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. 如果.dll以粗体显示,则会出现错误,提示您无法运行类库,而必须将其更改为控制台应用程序。 If it is a console application then it must have a static void Main() function defined so it knows where to start. 如果它是一个控制台应用程序,那么它必须定义一个静态的void Main()函数,以便它知道从哪里开始。 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 . 您不需要使用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 . 由于common.dll是托管程序集,因此您只需要在尝试使用NewString的ASP.NET项目中将其添加为引用即可。

if your assembly is written in c#, there's no need for a dllimport. 如果您的程序集是用C#编写的,则不需要dllimport。 rather go for add references like so - msdn entry (go for browse ) 宁愿像这样添加引用-msdn条目 (用于浏览

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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