c# - Mono equivalent of ClientConnectionId -
i run function under mono ( current version 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 fails error :
error cs1061: type `system.data.sqlclient.sqlconnection' not contain definition `clientconnectionid' , no extension method `clientconnectionid' of type `system.data.sqlclient.sqlconnection' found. missing assembly reference?
what mono equivalent of clientconnectionid
? or how can fix it?
clientconnectionid
not implemented in mono sqlconnection class. if want have unique identifier each instance, can having id constructed hashcode example:
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 can use in method:
if (prop == "clientconnectionid") { return _conn.getclientconnectionid(); }
p.s.:
the hash code may repeated 2 different instances @ different points in time.
p.s.(2):
__monocs__
defined mono compiler. portion calling clientconnectionid
property wil not seen mono compiler , vice versa other portion , .net compiler.
p.s.(3):
another solution subclass sqlconnection
, implement clientconnectionid
it's sealed... , require subclassing other classes instantiate sqlconnection
class internally.
Comments
Post a Comment