2017-01-06 100 views
-1

我正在面臨問題,當我嘗試創建字典,其中鍵是System.Enum。問題是,像這樣的字典會分配垃圾,因爲默認的EqualityComparer不是最好的之一。我嘗試寫我自己的比較器,但沒有任何成功。它有可能嗎?比較器的System.Enum字典無拳擊

public enum MyEnum 
{ 
    One, Two, Three 
} 

public Dictionary<Enum, string> dict = new Dictionary<Enum, string>(); 

public void Test() 
{ 
    this.dict.Add(MyEnum.One, "One"); 
    this.dict.Add(MyEnum.Two, "Two"); 
    this.dict.Add(MyEnum.Three, "Three"); 

    string result; 
    this.dict.TryGetValue(MyEnum.Two, out result); // <-- memory alocation :-(
} 
+0

您的字典聲明中是否有拼寫錯誤?你的枚舉是'MyEnum',但你的字典是'Dictionary ' – maccettura

+1

爲什麼你的字典類型是''而不是''? –

+0

因爲我的字典存儲在ma基類中,並且Iam每次都使用不同的枚舉將其填充到派生類中 – MrIncognito

回答

2

在評論中看到您的示例,並且這是針對Unity的,請觀看this clip from Unite 2016。它討論使用腳本對象而不是字典鍵的枚舉。

你會做什麼是有

public class Program 
{ 
    protected Dictionary<ScriptableObject, string> dict = new Dictionary<ScriptableObject, string>(); 
} 

public class ProgramChild1 : Program 
{ 
    public void Test() 
    { 
     dict.Add(MyEnum1.One.Instance, "One"); 
     dict.Add(MyEnum1.Two.Instance, "Two"); 
     dict.Add(MyEnum1.Three.Instance, "Three"); 
     string result; 
     dict.TryGetValue(MyEnum1.Two.Instance, out result); 
    } 
} 

public class ProgramChild2 : Program 
{ 

    public void Test() 
    { 
     dict.Add(MyEnum2.Four.Instance, "One"); 
     dict.Add(MyEnum2.Five.Instance, "Two"); 
     dict.Add(MyEnum2.Six.Instance, "Three"); 
     string result; 
     dict.TryGetValue(MyEnum2.Five.Instance, out result); 
    } 
} 

//Each class goes in to its own .cs file, Put them in two folders `MyEnum1` and `MyEnum2` 
namespace MyEnum1 
{ 
    public class One : ScriptableObject 
    { 
     private static One _inst; 
     public static One Instance 
     { 
      get 
      { 
       if (!_inst) 
        _inst = Resources.FindObjectOfType<One>(); 
       if (!_inst) 
        _inst = CreateInstance<One>(); 
       return _inst; 
      } 
     } 
    } 
} 

namespace MyEnum1 
{ 
    public class Two : ScriptableObject 
    { 
     private static Two _inst; 
     public static Two Instance 
     { 
      get 
      { 
       if (!_inst) 
        _inst = Resources.FindObjectOfType<Two>(); 
       if (!_inst) 
        _inst = CreateInstance<Two>(); 
       return _inst; 
      } 
     } 
    } 
} 

namespace MyEnum1 
{  
    public class Three : ScriptableObject 
    { 
     private static Three _inst; 
     public static Three Instance 
     { 
      get 
      { 
       if (!_inst) 
        _inst = Resources.FindObjectOfType<Three>(); 
       if (!_inst) 
        _inst = CreateInstance<Three>(); 
       return _inst; 
      } 
     } 
    } 
} 

namespace MyEnum2 
{  
    public class Four : ScriptableObject 
    { 
     private static Four_inst; 
     public static Four Instance 
     { 
      get 
      { 
       if (!_inst) 
        _inst = Resources.FindObjectOfType<Four>(); 
       if (!_inst) 
        _inst = CreateInstance<Four>(); 
       return _inst; 
      } 
     } 
    } 
} 

namespace MyEnum2 
{  
    public class Five : ScriptableObject 
    { 
     private static Five _inst; 
     public static Five Instance 
     { 
      get 
      { 
       if (!_inst) 
        _inst = Resources.FindObjectOfType<Five>(); 
       if (!_inst) 
        _inst = CreateInstance<Five>(); 
       return _inst; 
      } 
     } 
    } 
} 

namespace MyEnum2 
{  
    public class Six : ScriptableObject 
    { 
     private static Six _inst; 
     public static Six Instance 
     { 
      get 
      { 
       if (!_inst) 
        _inst = Resources.FindObjectOfType<Six>(); 
       if (!_inst) 
        _inst = CreateInstance<Six>(); 
       return _inst; 
      } 
     } 
    } 
} 

請注意,我們從ScriptableObject繼承的原因是,如果你想一個枚舉暴露設計師,那麼你可以拖動和右鍵的在設計下降枚舉值,你不能這樣做,如果只是基本類的枚舉。

public class ProgramChild2 : Program 
{ 
    public ScriptableObject SelectedValue; 
    public void Test() 
    { 
     dict.Add(MyEnum2.Four.Instance, "One"); 
     dict.Add(MyEnum2.Five.Instance, "Two"); 
     dict.Add(MyEnum2.Six.Instance, "Three"); 
     string result; 
     dict.TryGetValue(SelectedValue, out result); 
    } 
}