2017-02-20 7 views
-1

インデントをクラス内で使用してリストを簡単に検索します。しかし、私はboolとintの両方を返すことができるようにしたい。同時にではなく、それがboolか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

boolを返すときとintを区別するにはどうすればよいでしょうか?どちらの場合も、同じ文字列引数を渡します。 – Evk

答えて

0

C#の過負荷解決メカニズムは戻り値を考慮しない。同じ名前と引数を持つ2つのメソッドを持つことはできません。戻り値の型だけが異なります。

インデクサーは単なる1つの方法(または2つ)なので、同じ規則に従います。同じ引数を持つインデクサを2つ持つことはできません。戻り値の型だけが異なります。

0

代わりに、辞書またはリスト型をそれぞれ含む2つの異なるプロパティを使用することをお勧めします。あなたはこのような何かを書くことができその方法:

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

代わりに「ハック」あなたはそこに使用することができます、非常によく動作します:

  • が実装2人の暗黙のキャストを持つクラスを作成します。 1つはint、もう1つはboolです。
  • 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#が(彼らは少なくとも1つのパラメータ型または型の引数の数が異なっている必要があります)あなたは純粋に彼らの戻り値の型に基づいて過負荷を区別することはできません。 ジェネリックメソッドを作成し、type引数を使用して2つのインテントを区別することができます。例えば。

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"); 
    } 
} 

、その後、あなたは明示的な型の引数でそれを呼びたい、C#は、汎用インデクサーをサポートしていないとして、このソリューションはthisインデクサーでは動作しないことなど

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

注意。

私は、これはあなたがしたいと思うようには聞こえないことに同意しますが、あなたの派遣は今、時間をコンパイルすると、あなたは確かに

uPermissions.GetAllowedOrCount<double>("foo") 

ようなタイプの安全性を得られないだろうとは対照的にランタイムですまた、コンパイルし、実行時例外を爆破でしょう。

同じ(またはオーバーロードされた)メソッドが2つの全く異なるものを返す/セットすること(つまり、許可されたものとカウントされるもの)が非常に混乱することがわかります。むしろ、私はむしろ2つの異なるメソッドを持つか、または単一のメソッドからデータ構造(許可/カウントのゲッターを持つ)を返すでしょう。後者の場合、インデクサーを使用することもできます。

関連する問題