我正在通过ILSPY从VideoFileWriter
程序集中读取AForge.Video.FFMPEG
类(我有兴趣了解特定方法的工作原理)并发现了这一点:
public bool IsOpen {
[return: MarshalAs(UnmanagedType.U1)]
get {
return ((this.data != null) ? 1 : 0) != 0;
}
}
是为什么将bool转换为整数而不是返回bool转换而不是
this.data != null
的原因? 最佳答案
它是反编译的代码,很可能只是反编译器的故障。
经过一番思考,这是一个合理的实现,可能会变成相同的已编译代码
public enum ConnectionState
{
Closed = 0,
Open = 1,
Opening = 2,
OtherStuff = 3,
AndSoOn = 4,
}
public bool IsOpen
{
get
{
ConnectionState state;
if (this.data != null)
{
state = ConnectionState.Open;
}
else
{
state = ConnectionState.Closed;
}
return state != ConnectionState.Closed;
}
}