2011-01-03 9 views
2

好奇心ではなく、メソッドを書く方法がありますか?.Netパラメータ属性名前/オプションのパラメータを内部/プライベートでのみ表示できるようにする属性?

public static MyType Parse(string stringRepresentation, [Internal] bool throwException = true) 
{ 
// parsing logic here that conditionally throws an exception or returns null ... 
} 

public static MyType TryParse(string stringRepresentation) 
{ 
return this.Parse(stringRepresentation, true); 
} 

内部的にコードの冗長性を削減したいと考えています。 (Try)Parse()のBCLメソッドのシグネチャですが、この場合、c#コンパイラが2番目の内部メソッドを生成することができればいいです。

これはどういうことですか?これまで何も見つかりませんでした。

答えて

3

私はあなたができるとは気づいていませんが、同じ結果が得られませんか?

public MyType Parse(string stringRepresentation) 
{ 
    return this.Parse(stringRepresentation, true); 
} 

internal MyType Parse(string stringRepresentation, bool throwException = true) 
{ 
    // parsing logic here that conditionally throws an exception or returns null ... 
} 
+0

うん、私がやっているもちろんのは、ちょうど私が公共/最初の定型的方法を切り出すことができるかどうかを疑問に思ったことそこに1つ入れてください。 –

+1

@JörgBオプションのパラメータでは、バージョニング(例:後でオーバーロードを追加するなど)が難しくなるため、パブリック/保護されたインターフェイスでプレーンなオーバーロードに固執するのは、すでに理由があります。 – Richard

1

これはちょっと遅い回答ですが、他の人にとっては役立つかもしれません。

アトリビュートクラスは、AttributeTargets.Parameterhere is the msdn link)でデコレートすることができます。これは正確に探しているものです。

サンプル属性:属性の

[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)] 
public class InternalAttribute : Attribute 
{ 
    // attribute code goes here 
} 

使用法:

public void Foo([Internal] type_of_parameter parameter_name) 
{ 
     //code 
} 
関連する問題