2011-01-03 2 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# 컴파일러가 좋은 두 번째 내부 메서드를 생성 할 수 있는지 확인합니다.

그건 이미 가능합니까? 지금까지 아무것도 찾을 수 없습니다.

답변

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

네 : 속성의

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

사용 거기에 하나 넣어. –

+1

@ JörgB 선택적 매개 변수를 사용하면 버전 관리 (예 : 나중에 과부하 추가)가 더 어려워 지므로 공개/보호 된 인터페이스에서 일반 오버로드를 고수해야하는 좋은 이유가 있습니다. – Richard

1

나는 조금 늦은 답변이지만 다른 사람에게 도움이 될 수 있음을 알고 있습니다.

당신은 당신이 찾고있는 정확하게이다 AttributeTargets.Parameter (here is the msdn link)와 속성 클래스를 decoreate 수 있습니다.

샘플 특성 : 내가 처음 공개/상용구 방법을 줄일 수 있는지 물론 내가 뭘하는지, 그냥 궁금을의의

public void Foo([Internal] type_of_parameter parameter_name) 
{ 
     //code 
} 
관련 문제