С#。错误 "Expected method with different signature"。但显示相同的签名
С#. Error "Expected method with different signature". But show same signature
我在 Program.cs
中有事件处理程序方法 c_CriticalSituationHappened
void c_CriticalSituationHappened(object sender, CriticalSituationGacEventArgs e)
{
if (e.TypeDisrepair != TypeDisrepairGac.None)
{
start_time = DateTime.UtcNow;
typeDisrepair = e.TypeDisrepair;
WriteLine("Crit sit time: " + start_time);
isStarted = true;
}
}
如果我将它移动到 class 库,我会导入它。
public class CorrectBehaviorCheck
{
public void CriticalSituationHappened1(object sender, CriticalSituationGacEventArgs e)
{
if (e.TypeDisrepair != TypeDisrepairGac.None)
{
criticalSituationStartTime = DateTime.Now;
TypeDisrepair = e.TypeDisrepair;
Console.WriteLine("111");
isStarted = true;
}
}
}
签名完全相同。
但是 如果尝试
using cbc = mymodule.CorrectBehaviorCheck;
class Program{
static void Main(string[] args){
cbc a = new cbc();
CriticalSituationHappened += a.CriticalSituationHappened1;
}
}
代替
class Program{
static void Main(string[] args){
CriticalSituationHappened += c_CriticalSituationHappened;
}
}
我有一个错误Expected a method with 'void CriticalSituationHappened1(object?, CriticalSituationGacEventArgs)' signature
我做错了什么?这两种方法具有相同的签名。为什么当我将方法放入 class 库时程序会中断?
在 Program.cs:
protected virtual void OnCriticalSituationHappened(CriticalSituationGacEventArgs e)
{
EventHandler<CriticalSituationGacEventArgs> handler = CriticalSituationHappened;
if (handler != null)
{
handler(this, e);
}
}
public static event EventHandler<CriticalSituationGacEventArgs> CriticalSituationHappened;
您的错误是由于 CriticalSituationGacEventArgs
跨程序集的多个定义而发生的。为了说明一点,在主程序集中(我们称之为 A
)我们有:
namespace A
{
using cbc = B.CorrectBehaviorCheck;
public class CriticalSituationGacEventArgs : EventArgs
{
public string Message { get; set; }
}
public class Program
{
public static event EventHandler<A.CriticalSituationGacEventArgs> CriticalSituationHappened;
public static void Main(string[] args)
{
cbc a = new cbc();
CriticalSituationHappened += a.CriticalSituationHappened1;
CriticalSituationHappened(null, new A.CriticalSituationGacEventArgs { Message = "Hello!" });
}
}
}
然后在外部程序集(我们称之为 B
)中我们有:
namespace B
{
public class CriticalSituationGacEventArgs : EventArgs
{
public string Message { get; set; }
}
public class CorrectBehaviorCheck
{
public void CriticalSituationHappened1(object sender, B.CriticalSituationGacEventArgs e)
{
Console.WriteLine("Listening in CorrectBehaviorCheck.CriticalSituationHappened1: " + e.Message);
}
}
}
这会导致 CriticalSituationHappened += a.CriticalSituationHappened1
处出现错误。为了更深入地理解这一点,我们可以更明确地将其定义为:
CriticalSituationHappened += (object sender, A.CriticalSituationGacEventArgs e) => a.CriticalSituationHappened1(sender, e);
这试图将 A.CriticalSituationGacEventArgs
传递给期望 B.CriticalSituationGacEventArgs
的参数,这给出了一个错误:
CS1503 Argument 2: cannot convert from 'A.CriticalSituationGacEventArgs' to 'B.CriticalSituationGacEventArgs'
因为编译器无法知道如何从一种类型转换为另一种类型。鉴于您的情况,有几种选择可以解决此问题:
1。手动转换事件参数
CriticalSituationHappened += (sender, e) =>
a.CriticalSituationHappened1(sender, new B.CriticalSituationGacEventArgs
{
Message = e.Message,
// set other properties
});
2。在共享程序集中定义一次事件参数
创建一个新项目(例如MyEventArgs
)并在该项目中定义一次CriticalSituationGacEventArgs
。那么A
和B
在定义事件(在A
中)和处理事件(在B
中)都可以引用MyEventArgs.CriticalSituationGacEventArgs
。这也避免了在 A
或 B
.
中定义单一类型的循环依赖
我在 Program.cs
c_CriticalSituationHappened
void c_CriticalSituationHappened(object sender, CriticalSituationGacEventArgs e)
{
if (e.TypeDisrepair != TypeDisrepairGac.None)
{
start_time = DateTime.UtcNow;
typeDisrepair = e.TypeDisrepair;
WriteLine("Crit sit time: " + start_time);
isStarted = true;
}
}
如果我将它移动到 class 库,我会导入它。
public class CorrectBehaviorCheck
{
public void CriticalSituationHappened1(object sender, CriticalSituationGacEventArgs e)
{
if (e.TypeDisrepair != TypeDisrepairGac.None)
{
criticalSituationStartTime = DateTime.Now;
TypeDisrepair = e.TypeDisrepair;
Console.WriteLine("111");
isStarted = true;
}
}
}
签名完全相同。 但是 如果尝试
using cbc = mymodule.CorrectBehaviorCheck;
class Program{
static void Main(string[] args){
cbc a = new cbc();
CriticalSituationHappened += a.CriticalSituationHappened1;
}
}
代替
class Program{
static void Main(string[] args){
CriticalSituationHappened += c_CriticalSituationHappened;
}
}
我有一个错误Expected a method with 'void CriticalSituationHappened1(object?, CriticalSituationGacEventArgs)' signature
我做错了什么?这两种方法具有相同的签名。为什么当我将方法放入 class 库时程序会中断?
在 Program.cs:
protected virtual void OnCriticalSituationHappened(CriticalSituationGacEventArgs e)
{
EventHandler<CriticalSituationGacEventArgs> handler = CriticalSituationHappened;
if (handler != null)
{
handler(this, e);
}
}
public static event EventHandler<CriticalSituationGacEventArgs> CriticalSituationHappened;
您的错误是由于 CriticalSituationGacEventArgs
跨程序集的多个定义而发生的。为了说明一点,在主程序集中(我们称之为 A
)我们有:
namespace A
{
using cbc = B.CorrectBehaviorCheck;
public class CriticalSituationGacEventArgs : EventArgs
{
public string Message { get; set; }
}
public class Program
{
public static event EventHandler<A.CriticalSituationGacEventArgs> CriticalSituationHappened;
public static void Main(string[] args)
{
cbc a = new cbc();
CriticalSituationHappened += a.CriticalSituationHappened1;
CriticalSituationHappened(null, new A.CriticalSituationGacEventArgs { Message = "Hello!" });
}
}
}
然后在外部程序集(我们称之为 B
)中我们有:
namespace B
{
public class CriticalSituationGacEventArgs : EventArgs
{
public string Message { get; set; }
}
public class CorrectBehaviorCheck
{
public void CriticalSituationHappened1(object sender, B.CriticalSituationGacEventArgs e)
{
Console.WriteLine("Listening in CorrectBehaviorCheck.CriticalSituationHappened1: " + e.Message);
}
}
}
这会导致 CriticalSituationHappened += a.CriticalSituationHappened1
处出现错误。为了更深入地理解这一点,我们可以更明确地将其定义为:
CriticalSituationHappened += (object sender, A.CriticalSituationGacEventArgs e) => a.CriticalSituationHappened1(sender, e);
这试图将 A.CriticalSituationGacEventArgs
传递给期望 B.CriticalSituationGacEventArgs
的参数,这给出了一个错误:
CS1503 Argument 2: cannot convert from 'A.CriticalSituationGacEventArgs' to 'B.CriticalSituationGacEventArgs'
因为编译器无法知道如何从一种类型转换为另一种类型。鉴于您的情况,有几种选择可以解决此问题:
1。手动转换事件参数
CriticalSituationHappened += (sender, e) =>
a.CriticalSituationHappened1(sender, new B.CriticalSituationGacEventArgs
{
Message = e.Message,
// set other properties
});
2。在共享程序集中定义一次事件参数
创建一个新项目(例如MyEventArgs
)并在该项目中定义一次CriticalSituationGacEventArgs
。那么A
和B
在定义事件(在A
中)和处理事件(在B
中)都可以引用MyEventArgs.CriticalSituationGacEventArgs
。这也避免了在 A
或 B
.