中国开发网: 论坛: 程序员情感CBD: 贴子 633117
Miracle
小伙子,看看这个是不是你想要的。VS2008里面简单测试了一下,应该符合你的需求
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KissofTheDragon.cndev.org
{
class Program
{
static void Main(string[] args)
{
ServiceFactory<ServiceClientWrapper<Service1Client>>.GetInstance().Abort();
ServiceFactory<ServiceClientWrapper<Service2Client>>.GetInstance().Abort();
Console.WriteLine("Press enter to continue. The constructors should not be called again since you applied singleton pattern.");
Console.ReadLine();
ServiceFactory<ServiceClientWrapper<Service1Client>>.GetInstance().Abort();
ServiceFactory<ServiceClientWrapper<Service2Client>>.GetInstance().Abort();
Console.WriteLine("Oh yeah, press enter to quit.");
Console.ReadLine();
}
}

/// <summary>
/// This is the factory method that you should call in the consumer code
/// </summary>
/// <typeparam name="T">The ServiceClient class</typeparam>
public static class ServiceFactory<T>
{
public static ServiceClientWrapper<T> GetInstance()
{
if (Singleton<ServiceClientWrapper<T>>.Instance.State == CommunicationState.Faulted || Singleton<ServiceClientWrapper<T>>.Instance.State == CommunicationState.Closed)
{
Singleton<ServiceClientWrapper<T>>.Instance.Abort();
Singleton<ServiceClientWrapper<T>>.Instance = default(ServiceClientWrapper<T>);
}
if (Singleton<ServiceClientWrapper<T>>.Instance.ClientCredentials.UserName == null)
{
Singleton<ServiceClientWrapper<T>>.Instance.ClientCredentials.UserName = "abc";
Singleton<ServiceClientWrapper<T>>.Instance.ClientCredentials.Password = "************";
}
return Singleton<ServiceClientWrapper<T>>.Instance;
}
}

/// <summary>
/// Use this generic class to utilize singleton!
/// </summary>
/// <typeparam name="T">Class you want to make it "lonely" - you are so evil</typeparam>
public class Singleton<T>
{
private static T _instance;
public Singleton()
{
}
public static T Instance
{
get
{
if (_instance == null)
{
//获得实例,使用这个方法的前提是T要有公有的、无参数的构造函数        
_instance = (T)System.Activator.CreateInstance(typeof(T));

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

/// <summary>
/// This wrapper class contains the common behaviors across different ServiceClient class
/// </summary>
/// <typeparam name="T">The ServiceClient class. You need to make sure they all support State, Abort and ClientCredentials properly</typeparam>
public class ServiceClientWrapper<T>
{
object _instance = null;
Type type = null;
public ServiceClientWrapper()
{
type = typeof(T);
_instance = System.Activator.CreateInstance(type);
}

#region Common behaviors across different ServiceClient classes
public CommunicationState State
{
get
{
return (CommunicationState)type.GetProperty("State").GetValue(_instance, null);
}
set
{
type.GetProperty("State").SetValue(_instance, value, null);
}
}

public void Abort()
{
type.InvokeMember("Abort", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance
| System.Reflection.BindingFlags.InvokeMethod, null, _instance, null);
}

public UserIdentity ClientCredentials
{
get
{
return (UserIdentity)type.GetProperty("ClientCredentials").GetValue(_instance, null);
}
set
{
type.GetProperty("ClientCredentials").SetValue(_instance, value, null);
}
}

#endregion
}

#region I created these classes in order to run the code in Main()
public enum CommunicationState
{
Established,
Faulted,
Closed
}

public class UserIdentity
{
public string UserName { get; set; }
public string Password { get; set; }
}

public class Service1Client
{
public Service1Client()
{
State = CommunicationState.Established;
ClientCredentials = new UserIdentity();
Console.WriteLine("Creating Service1Client...");
}
public CommunicationState State { get; set; }
public void Abort() { Console.WriteLine("===> Service1Client.Abort() is called."); }
public UserIdentity ClientCredentials { get; set; }
}

public class Service2Client
{
public Service2Client()
{
State = CommunicationState.Established;
ClientCredentials = new UserIdentity();
Console.WriteLine("Creating Service2Client...");
}
public CommunicationState State { get; set; }
public void Abort() { Console.WriteLine("===> Service2Client.Abort() is called."); }
public UserIdentity ClientCredentials { get; set; }
}
#endregion

}
夫习拳艺者,对已者十之七八,对人者,仅十之二三耳。拳艺之道,深无止境。得其浅者,一人敌,得其深者,何尝不万人敌耶!
我的Google Picasa相册
我的新BLOG

相关信息:


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