2017-02-20 133 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)。在后一种情况下,如果您喜欢,也可以使用索引器。