我多次复制/粘贴了以下代码。更改的值为字符串文字(“ TabStates”更改为“ ContentStates”等。)和字典的值(RadTabSetting-> ContentSetting)。

public static SerializableDictionary<string, RadTabSetting> GetTabStates()
{
    SerializableDictionary<string, RadTabSetting> _tabStates = new SerializableDictionary<string, RadTabSetting>();

    if (!object.Equals(DashboardSessionRepository.Instance.GetSession("TabStates"), null))
    {
        _tabStates = DashboardSessionRepository.Instance.GetSession("TabStates") as SerializableDictionary<string, RadTabSetting>;
    }
    else
    {
        XmlSerializer serializer = new XmlSerializer(_tabStates.GetType());

        string data = DashoardDatabaseRepository.Instance.GetWebLayoutData("TabStates");

        if ( !string.IsNullOrEmpty(data) )
        {
            byte[] dataAsArray = Convert.FromBase64String(data);
            MemoryStream stream = new MemoryStream(dataAsArray);
            _tabStates = serializer.Deserialize(stream) as SerializableDictionary<string, RadTabSetting>;
            }
            DashboardSessionRepository.Instance.SetSession("TabStates", _tabStates);
        }

        return _tabStates;
    }

    public static void SetTabStates(SerializableDictionary<string, RadTabSetting> tabStates)
    {
        DashboardSessionRepository.Instance.SetSession("TabStates", tabStates);
        DashboardDatabaseRepository.Instance.SaveToDatabase("TabStates", tabStates);
    }


我不是在寻找答案,只是好奇我应该读什么以学习如何重写它。我敢肯定它很简单,只是不确定它叫什么。它只是函数模板吗?

    public static T GetStates<T>() where T: new()
    {
        T _states = new T();//(T)Activator.CreateInstance(typeof(T));

        string stateName = StateDictionary.GetStateName(typeof(T));

        if (!object.Equals(DashboardSessionRepository.Instance.GetSession(stateName), null))
        {
            _states = (T)DashboardSessionRepository.Instance.GetSession(stateName);

            //Work-Around
            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
            System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(_states.GetType());
            xmlSerializer.Serialize(memoryStream, _states);
            string data = System.Convert.ToBase64String(memoryStream.ToArray());
            string otherData = DashboardDatabaseRepository.Instance.GetWebLayoutData(stateName);

            if (!string.IsNullOrEmpty(data))
            {
                XmlSerializer serializer = new XmlSerializer(_states.GetType());
                byte[] dataAsArray = Convert.FromBase64String(data);
                MemoryStream stream = new MemoryStream(dataAsArray);

                _states = (T)serializer.Deserialize(stream);
            }
            //Work-Around
        }
        else
        {
            XmlSerializer serializer = new XmlSerializer(_states.GetType());

            string data = DashboardDatabaseRepository.Instance.GetWebLayoutData(stateName);

            if (!string.IsNullOrEmpty(data))
            {
                byte[] dataAsArray = Convert.FromBase64String(data);
                MemoryStream stream = new MemoryStream(dataAsArray);
                _states = (T)serializer.Deserialize(stream);
            }
            DashboardSessionRepository.Instance.SetSession(stateName, _states);
        }

        return _states;
    }

    public static void SetStates<T>(T states) where T: new()
    {
        string stateName = StateDictionary.GetStateName(typeof(T));

        DashboardSessionRepository.Instance.SetSession(stateName, states);
        DashboardDatabaseRepository.Instance.SaveToDatabase(stateName);
    }

static class StateDictionary
{
    //TODO: Might (should?) be able to redo this polymorphically.
    static IDictionary<Type, string> _stateDictionary = new Dictionary<Type, string>
    {
        {typeof(SerializableDictionary<string, RadTabSetting>), "TabStates"},
        {typeof(SerializableDictionary<string, RadDockContentSetting>), "ContentStates"},
        {typeof(SerializableDictionary<string, RadPaneSetting>), "PaneStates"},
        {typeof(SerializableDictionary<string, RadDockSetting>), "DockStates"},
        {typeof(SerializableDictionary<string, RadDockZoneSetting>), "DockZoneStates"},
        {typeof(SerializableDictionary<string, RadSplitterSetting>), "SplitterStates"},
        {typeof(SerializableDictionary<string, RadSplitBarSetting>), "SplitBarStates"},
        {typeof(KnownGlobalSettings), "GlobalSettings"},
    };

    public static string GetStateName(Type type)
    {
        string stateName = string.Empty;

        if (_stateDictionary.ContainsKey(type))
        {
            stateName = _stateDictionary[type];
        }

        return stateName;
    }
}

最佳答案

Martin Fowler's book是重构的标准来源。

但是对于您的特定示例,只需创建一个新方法,该方法采用代表示例中发生更改的事物的参数。因此,您需要一个用于“ TabStates”或“ ContentStates”值的字符串参数,以及一个用于表示字典中Key ContentSetting值的字符串参数。

那有意义吗?我是否完全理解您的问题?

编辑

根据您的评论,您想使用泛型。这样的事情应该可以帮助您:

public static Dictionary<string, T> GetTabStates<T>()


请注意,除非添加一些通用约束,否则您将无法对T类型做很多事情。

如果要创建T的新实例,则需要

public static Dictionary<string, T> GetTabStates<T>() where T : new() {


而且,如果您想访问T实例上的实际属性,那么希望T的所有可能值都将实现某种接口,在这种情况下,您应该说:

public interface IFoo {
    int Id { get; set; }
}

public static Dictionary<string, T> GetTabStates<T>() where T : new(), IFoo {
   T Tval = new T();
   Tval.Id = 1;
   //etc

07-27 13:19