2017-02-20 134 views
-1

我在我的類中使用索引器來更容易地搜索列表。但是,我希望能夠返回一個布爾和一個int。不是在同一時間,而是確定它是一個布爾型還是int型,然後返回它。具有多種返回類型的索引器

public class uPermissions 
{ 

    private List<uPermission> permissions = new List<uPermission>() 
    { 
     new uPermission ("canBuild", false), 
     new uPermission ("canClaim", false), 
     new uPermission ("canUnClaim", false), 
     new uPermission ("canInvite", false), 
     new uPermission ("canKick", false), 
     new uPermission ("canSetHome", false) 
    }; 
    private List<uConstraint> constraints = new List<uConstraint>(); 

    public bool this[string index] 
    { 
     get 
     { 
      return permissions.Where (p => p.name == index).First().allowed; 
     } 
     set 
     { 
      permissions.Where (p => p.name == index).First().allowed = value; 
     } 
    } 
    public int this[string index] 
    { 
     get 
     { 
      return constraints.Where (c => c.name == index).First().count; 
     } 
     set 
     { 
      constraints.Where (c => c.name == index).First().count = value; 
     } 
    } 

    public bool exists (string permission) 
    { 
     var perm = permissions.Where (p => p.name.ToLower() == permission.ToLower()).First(); 
     return (perm != null) ? true : false; 
    } 

    public void setAllTrue() 
    { 
     foreach (uPermission p in permissions) 
     { 
      p.allowed = true; 
     } 
    } 
    public void setAllFalse() 
    { 
     foreach (uPermission p in permissions) 
     { 
      p.allowed = false; 
     } 
    } 
} 

public class uConstraint 
{ 
    public string name; 
    public int count; 

    public uConstraint() { } 
    public uConstraint (string name, int count) 
    { 
     this.name = name; 
     this.count = count; 
    } 
} 

public class uPermission 
{ 
    public string name; 
    public bool allowed; 

    public uPermission() { } 
    public uPermission (string name, bool allowed) 
    { 
     this.name = name; 
     this.allowed = allowed; 
    } 
} 

這是我的代碼。我在搜索時看到了一些關於模板的內容,但我不明白它是如何工作的,或者它甚至是正確的解決方案。如果有人可以提供一些見解,將不勝感激

+0

也許你應該考慮字典 - 它會更快。 –

+0

我會使用字典,但這是對另一個軟件的擴展,它使用xml來存儲數據,但xml解析並不解析字典 –

+0

那麼,如何區分何時應該返回布爾,何時int?你在這兩種情況下都傳遞相同的字符串參數。 – Evk

回答

0

C#中的重載解析機制不考慮返回值。不能有兩個具有相同名稱和參數的方法,只有返回類型不同。

索引器只是一個方法(或兩個),所以它遵循相同的規則。不能有兩個具有相同參數的索引器,只有返回類型不同。

0

我建議使用兩個不同的屬性,每個屬性都包含一個字典或一些List類型。這樣,你可以寫這樣的事情:

bool foo = uPermissionsInstance.Permissions["someName"]; 
int bar = uPermissionsInstance.Constraints["someOtherName"]; 

或者有一個「黑客」,你可以用,也工作得很好:

  • 寫有實現的兩個隱式轉換一類。一個用於int,另一個用於布爾。
  • 返回此類而不是int或bool並適當地設置其內部值。

這樣你可以寫下面的內容。但這不適用於二傳手!

bool foo = uPermissionsInstance["someName"]; 
int bar = uPermissionsInstance["someOtherName"]; 

實現:

public IntBoolValue this[string index] 
{ 
    get 
    { 
     // Here you need to check whether you can find an entry in permissions or in constraints. Then return the found value. 
     return new IntBoolValue(permissions.Where (p => p.name == index).First().allowed); 
    } 
} 

// ------------------ 

internal struct IntBoolValue 
{ 
    internal int internalInt; 
    internal bool internalBool; 

    public IntBoolValue(int value) { this.internalInt = value; } 
    public IntBoolValue(bool value) { this.internalBool = value; } 

    public static implicit operator bool(IntBoolValue value) 
    { 
     return value.internalBool; 
    } 

    public static implicit operator int(IntBoolValue value) 
    { 
     return value.internalInt; 
    } 
} 
0

C#不會讓你區分完全基於它們的返回類型(必須至少在一個參數類型或類型參數的數量不同)重載。 您可以創建一個通用方法並使用類型參數來區分您的兩個意圖。例如。

class uPermissions { 
    public T GetAllowedOrCount<T>(string index) { 
    get { 
     if (typeof(T) == typeof(bool) { 
     return permissions.Where (p => p.name == index).First().allowed; 
     } else if (typeof(T) == typeof(int) { 
     return constraints.Where (c => c.name == index).First().count; 
     } 
     throw new InvalidOperationException("only bool and int are supported"); 
    } 
} 

,然後你用一個明確的類型參數調用它,如

var fooCount = uPermissions.GetAllowedOrCount<int>("foo") 
var fooAllowed = uPermissions.GetAllowedOrCount<string>("foo") 

請注意,此解決方案將無法與this索引工作爲C#不支持通用索引。

我想盡管我們同意這聽起來不像你想做的事情。您的調度現在運行時,而不是編譯時,你肯定不會獲得類型安全的

uPermissions.GetAllowedOrCount<double>("foo") 

也將編譯和運行時異常炸燬。

說實話,我也會發現,同樣的(或重載的)方法返回/設置兩個完全不同的東西(即允許與數量)非常混淆。我寧願有兩種不同的方法,或者從一個方法返回一個數據結構(它有一個get/count的getter)。在後一種情況下,如果您喜歡,也可以使用索引器。