gpt4 book ai didi

c# - 最佳重载方法匹配...有一些无效参数 [byref/byval]

转载 作者:行者123 更新时间:2023-11-30 23:14:53 24 4
gpt4 key购买 nike

我正在从 asp.net 应用程序调用一个 dll(用 vb6 编写)。我正在调用 dll 的以下函数:

Public Function FamIdentify_BYTE(strName As String, strDatabaseTemplate As Variant, len_of_data As Long, strIdentifyTemplate As String, strIP As String) As Boolean

以下 C# 代码调用上述函数:

public Boolean VerifyFinger(String name , String SampleModel, String REMOTE_ADDR)
{

try
{
Connect();
OracleCommand command = connection.CreateCommand();
string sql = "SELECT FINGER_DATA,DATA_LENGTH,SERIAL_NO FROM FP_BIOMETRIC_DATA WHERE CUST_NO =" + name.Trim();
command.CommandText = sql;
OracleDataReader reader = command.ExecuteReader();

bool fingerMatched = false;
FamServer.FamApp famApp = new FamServer.FamApp();
while (reader.Read())
{
Object[] values = new object[3];
int numColumns = reader.GetValues(values);
byte[] fingerData = values[0] as byte[];
string result = System.Text.Encoding.UTF8.GetString(fingerData);

int length_of_data = Convert.ToInt32(values[1]);
int serial_no = Convert.ToInt32(values[2]);

// FamIdentify_BYTE(ref string, ref object, ref int, ref string, ref string)
fingerMatched = famApp.FamIdentify_BYTE(name, fingerData, length_of_data, SampleModel, REMOTE_ADDR);

// if (fingerMatched)
// break;
}
famApp.Termination();
// famApp = null;
GC.Collect();

Close();
return fingerMatched;
}
catch (Exception e)
{
HttpContext.Current.Response.Write("<br>" + e.GetBaseException() + "<br>");
return false;
}
}

但是当我在 IIS express 中运行应用程序时,出现了这个错误:

The best overloaded method match for 'FamServer._FamApp.FamIdentify_BYTE(ref string, ref object, ref int, ref string, ref string)'
has some invalid arguments ASPNET(1)

这是完整的错误列表:

F:\ASP.NET Projects\ASPNET\App_Code\Global.asax.cs(180,37): error CS1502: The best overloaded method match for 'FamServer._FamApp.FamIdentify_BYTE(ref string, ref object, ref int, ref string, ref string)' has some invalid arguments
F:\ASP.NET Projects\ASPNET\App_Code\Global.asax.cs(180,61): error CS1620: Argument '1' must be passed with the 'ref' keyword
F:\ASP.NET Projects\ASPNET\App_Code\Global.asax.cs(180,67): error CS1503: Argument '2': cannot convert from 'byte[]' to 'ref object'
F:\ASP.NET Projects\ASPNET\App_Code\Global.asax.cs(180,79): error CS1620: Argument '3' must be passed with the 'ref' keyword
F:\ASP.NET Projects\ASPNET\App_Code\Global.asax.cs(180,95): error CS1620: Argument '4' must be passed with the 'ref' keyword
F:\ASP.NET Projects\ASPNET\App_Code\Global.asax.cs(180,108): error CS1620: Argument '5' must be passed with the 'ref' keyword

如果我在 IIS 服务器上运行上面的应用程序,重定向时间太长了。我不明白为什么会这样。请帮我 。

最佳答案

关于 MSDN Library 的传递参数机制的一个小问题:

In Visual Basic 6.0, if you do not specify ByVal or ByRef for a procedure parameter, the passing mechanism defaults to ByRef. This allows the variable passed into the procedure to be modified in the calling program.

使用上面的解释,默认情况下,DLL 库方法对所有输入变量使用按引用传递,这与 C# 的默认值(而不是按值传递)具有不同的行为。因此,ref 关键字对于方法调用中的每个传递变量都是强制性的,如下所示:

fingerMatched = famApp.FamIdentify_BYTE(ref name, ref fingerData, ref length_of_data, ref SampleModel, ref REMOTE_ADDR);

在这一点上,它似乎工作正常但仍然有第二个问题,它包含在传递输入参数期间从字节数组到 object(VB 6 中的 Variant)的无效转换.您必须先将该字节数组转换为 object:

byte[] fingerData = values[0] as byte[];

// Variant in VB 6 translated as Object into C#
// Therefore, a conversion to Object type is required
object fingerObject = fingerData;

那么,上面的方法调用应该改成这样:

fingerMatched = famApp.FamIdentify_BYTE(ref name, ref fingerObject, ref length_of_data, ref SampleModel, ref REMOTE_ADDR);

注意:您不能调用带有引用参数的方法,而不是完全指定的参数类型,并且ref参数必须包含任何可分配的值。

相关引用:

Passing Objects By Reference or Value in C#

Why use the 'ref' keyword when passing an object?

Passing Parameters (MSDN Library)

Cannot convert from 'ref byte[]' to 'ref object'

关于c# - 最佳重载方法匹配...有一些无效参数 [byref/byval],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42883198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com