中国开发网: 论坛: 程序员情感CBD: 贴子 633709
qyuhen: 你的代码是不是搞复杂了,我试着改了改,不一定对。
public class Singleton<T>
where T : class, new()
{
private static T _instance;

public Singleton()
{
}

public static T Instance
{
get
{
if (_instance == null)
{
_instance = new T();
}

Type type = typeof(T);
CommunicationState state = (CommunicationState)type.GetProperty("State").GetValue(_instance, null);
ClientCredentials clientCredential = (ClientCredentials)type.GetProperty("ClientCredentials").GetValue(_instance, null);

if (state == CommunicationState.Faulted || state == CommunicationState.Closed)
{
LoggingService.Debug(String.Format("\t通讯错误,重新创建!{0}", type.Name));

type.InvokeMember("Abort", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, _instance, null);
_instance = null;
}

if (clientCredential.UserName.UserName == null)
{
clientCredential.UserName.UserName = WCFService.UserName;
clientCredential.UserName.Password = WCFService.Password;
}

return _instance;
}
set
{
_instance = value;
}
}
}

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录