최근 나 자신이 점점 더 많은 것들을 참조로 전달하는 습관을 갖게된다는 것을 알게되었습니다. 저는 항상 심판을 통과하는 것이 '보통'나쁜 생각인데, 당신의 물건에 영향을 미칠 수있는 것을 추적하는 것이 더 까다롭기 때문에 질문을 게시하고 싶었습니다 : '참조로 전달할 때의 단점은 무엇입니까? ? '참조로 전달할 때의 단점은 무엇입니까?
최근에 참조로 전달한 예제는 viewstate 내에 지연 인스턴스화 된 개체입니다. 코드 숨김 내에서 공용 속성이있는 개인 필드가 있는데 이는 도우미 메서드를 사용합니다.
ASPX 코드 숨김
/// <summary>
/// Private field member for MyObject
/// </summary>
private Foobar _myObject = null;
/// <summary>
/// Gets or sets the current object
/// </summary>
public Foobar MyObject
{
get
{
return this.ViewState.GetValue("MyObject", new Foobar(), ref this._myObject);
}
set
{
this.ViewState.SetValue("MyObject", value, ref this._myObject);
}
}
이이 클래스 내의 필드와 게으른 인스턴스화하는 물체로부터 반복적 인 if
할당 검사를 많이 대체하는 것을 목표로 다음과 같이 구현하는 현재입니다. 예를 들어 도우미 클래스가 없다면 비슷한 것입니다.
/// <summary>
/// Private field member for MyObject
/// </summary>
private Foobar _myObject = null;
/// <summary>
/// Gets or sets the current object
/// </summary>
public Foobar MyObject
{
get
{
if (this._myObject != null)
{
return this._myObject;
}
var viewStateValue = this.ViewState["MyObject"];
if (viewStateValue == null || !(viewStateValue is Foobar))
{
this.ViewState["MyObject"] = new Foobar();
}
return this._myObject = (Foobar)this.ViewState["MyObject"];
}
set
{
this._myObject = value;
this.ViewState["MyObject"] = value;
}
}
코드 스 니펫은 모두 동일합니다. 첫 번째 방법은 모든 것을 중앙 집중화하는 것인데, 좋은 일이지만 참조로 전달하는 것입니다.이 경우에는 좋은 아이디어가 아닌 것인지 잘 모르겠습니다.
모든 조언 및/또는 경험을 높이 평가합니다.
편집 GetValue
및 SetValue
은의 ViewState에 확장 방법이 있습니다. 코드는 아래에 제공됩니다.
/// <summary>
/// Gets a value from the current view state, if the type is correct and present
/// </summary>
public static T GetValue<T>(this StateBag source, string key, T @default)
{
// check if the view state object exists, and is of the correct type
object value = source[key];
if (value == null || !(value is T))
{
return @default;
}
// return the object from the view state
return (T)source[key];
}
/// <summary>
/// Sets the key value within the view state
/// </summary>
public static void SetValue<T>(this StateBag source, string key, T value)
{
source[key] = value;
}
/// <summary>
/// Gets a value from the reference field helper, or the current view state, if the type is correct and present
/// </summary>
/// <returns>Returns a strongly typed session object, or default value</returns>
public static T GetValue<T>(this StateBag source, string key, T @default, ref T fieldHelper)
{
return fieldHelper != null ? fieldHelper : fieldHelper = source.GetValue(key, @default);
}
/// <summary>
/// Sets the key value within the view state and the field helper
/// </summary>
/// <param name="value">The value</param>
public static void SetValue<T>(this StateBag source, string key, T value, ref T fieldHelper)
{
source[key] = value;
fieldHelper = value;
}
현재 'ViewState.GetValue (...);'에 체크와 게으른 인스턴스가 없다면? 왜'Foobar' 클래스에 제네릭 메소드를 가질 수 없습니까? – Default
조금 이상해 보입니다. 왜 당신은'_myObject'에 한 번,'this.ViewState [ "MyObject"]'에 한 번 같은 객체를 두 번 저장하고 싶습니까? 첫 번째'get'은 리턴을 전혀 가지고 있지 않으며, 당신이 그것을 사용하든 안하든 항상 새로운 객체를 인스턴스화합니다. – martinstoeckli
가능한 복제본 : http://stackoverflow.com/questions/570471/whats-so-bad-about-ref-parameters – Default