[英]Mono equivalent of ClientConnectionId
I would like to run this function under mono ( my current version is 4.0.2) 我想在mono下运行这个功能(我目前的版本是4.0.2)
public Object GetConnectionProperty(SqlConnection _conn, string prop)
{
if (_conn.State == ConnectionState.Closed &
prop == "ServerVersion")
return string.Empty;
if (prop == "ClientConnectionId")
{
Guid guid = _conn.ClientConnectionId;
return guid.ToString();
}
return _conn.GetType().GetProperty(prop).GetValue(_conn);
}
But it fails with the error : 但它失败了,错误:
error CS1061: Type `System.Data.SqlClient.SqlConnection' does not contain a
definition for `ClientConnectionId' and no extension method
`ClientConnectionId' of type `System.Data.SqlClient.SqlConnection' could be
found. Are you missing an assembly reference?
What is the Mono equivalent of ClientConnectionId
? 什么是Mono等效的
ClientConnectionId
? Or how can I fix it? 或者我该如何解决?
ClientConnectionId
is not implemented in mono SqlConnection class. ClientConnectionId
未在单声道SqlConnection类中实现。 If you really want to have some unique identifier for each instance, you can do it yourself as having an id constructed from the hashcode for example: 如果你真的想为每个实例都有一些唯一的标识符,你可以自己做,因为有一个从哈希码构造的id,例如:
public static class SqlClientExtensions {
#if __MonoCS__
private static Dictionary<int, string> _connIds = new Dictionary<int, string>();
#endif
public static string GetClientConnectionId(this SqlConnection conn) {
if(conn == null) {
return Guid.Empty.ToString();
}
#if __MonoCS__
if(!connIds.ContainsKey(conn.GetHashCode())) {
connIds.Add(conn.GetHashCode(), Guid.NewGuid().ToString());
}
return connIds[conn.GetHashCode()];
#else
return conn.ClientConnectionId.ToString();
#endif
}
}
then you can use it in your method: 然后你可以在你的方法中使用它:
if (prop == "ClientConnectionId")
{
return _conn.GetClientConnectionId();
}
The hash code may be repeated for 2 different instances at different points in time. 可以在不同的时间点针对2个不同的实例重复哈希码。
__MonoCS__
is defined only by the mono compiler. __MonoCS__
仅由单声道编译器定义。 The portion calling the ClientConnectionId
property wil not be seen by mono compiler and vice versa for the other portion and .net compiler. 调用
ClientConnectionId
属性的部分不会被单声道编译器看到,反之亦然,对于其他部分和.net编译器。
Another solution would be to subclass SqlConnection
and implement ClientConnectionId
but it's sealed... And also that would require subclassing some other classes that instantiate the SqlConnection
class internally. 另一个解决方案是子类化
SqlConnection
并实现ClientConnectionId
但它是密封的...而且还需要子类化一些其他内部实例化SqlConnection
类的类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.