2013-02-27 151 views
0

我想在創建對象後修改它。我想將此對象的屬性設置爲-1,而對於字符串,則設置爲string.empty「」。波紋管是我已有的示例代碼。返回修改的泛型類型

class TestClassAccess{ 
    public int MyPropInt { get; set { ModifyOnAccessDenied<int>(value); } } 
    public string MyPropString { get; set { ModifyOnAccessDenied<string>(value); } } 

    public TestClassAccess() { } 

    private T ModifyOnAccessDenied<T>(T propertyToChange) { 
     var _hasAccess = false; //not really important how this is made 
     if (!_hasAccess) 
     { 
      if (propertyToChange is string) 
       propertyToChange = string.Empty; 
      else if (propertyToChange is int) 
       propertyToChange = -1; 
     }    
     return propertyToChange; 
    } 
} 

so ..我有問題。

  1. 它不編譯,因爲我無法將屬性轉換爲字符串或整型。
  2. 我不打結,如果我可以使用像這樣的設置方法。
  3. 這是可能的或我是雄心勃勃。

Thank.s KJ

+0

我修改了我的答案與要做到這一點的方法仿製藥。它需要.Net 4.0,但 – Corylulu 2013-02-27 04:30:21

回答

1

如果你在你可能做錯事的通用功能檢查特定的類型。在這種情況下,你可以很容易地只傳遞一個默認值,而不是它的硬編碼:

private T ModifyOnAccessDenied<T>(T newValue, T defaultValue) { 
    var _hasAccess = false; //not really important how this is made 
    if (!_hasAccess) 
    { 
     newValue = defaultValue; 
    }    
    return newValue; 
} 

我也改名爲propertyToChangenewValue,因爲你有這個功能有什麼新的價值,而不是一個屬性。

此外您的屬性定義不起作用。如果你需要在你的getter中包含任何邏輯或者設置你不能使用auto-initializer語法,並且必須實現帶有後臺字段的屬性。

+0

是的,你是對的,這是一個更好的主意,通過默認值與方法。謝謝。 – Kieran 2013-02-27 05:26:14

1

如果每個類型都需要特定的操作,似乎沒有必要使這個函數具有通用性。這似乎更合適。

class TestClassAccess 
    { 
     public int MyPropInt { get; set { ModifyOnAccessDenied<int>(value); } } 
     public string MyPropString { get; set { ModifyOnAccessDenied<string>(value); } } 

     public TestClassAccess() { } 

     private static volatile bool _hasAccess = false; 
     private string ModifyOnAccessDenied<string>(string propertyToChange) 
     { 
      if (!_hasAccess) 
       return string.Empty; 
      return propertyToChange; 
     } 
     private int ModifyOnAccessDenied<int>(int propertyToChange) 
     { 
      if (!_hasAccess) 
       return -1; 
      return propertyToChange; 
     } 
    } 

但你可以做到這一點使用力度,但這需要.NET 4.0

private T ModifyOnAccessDenied<T>(T propertyToChange) 
{ 
    if (!_hasAccess) 
    { 
     if (propertyToChange is string) 
      return (dynamic)string.Empty; 
     else if (propertyToChange is int) 
      return (dynamic)(int)-1; 
    } 
    return propertyToChange; 
} 

完全工作示例:

static class Program 
{ 
    [STAThread] 
    static void Main() 
    { 
     TestClassAccess test = new TestClassAccess(); 
     test.MyPropInt = 4; 
     test.MyPropString = "TEST"; 
     Console.WriteLine("MyPropInt {0}, MyPropString '{1}'",test.MyPropInt, test.MyPropString); 
     // Prints "MyPropInt -1, MyPropString '' 
    } 
    class TestClassAccess 
    { 
     private int myPropInt = 0; 
     public int MyPropInt { get { return myPropInt; } set { myPropInt = ModifyOnAccessDenied<int>(value); } } 
     private string myPropString = string.Empty; 
     public string MyPropString { get { return myPropString; } set { myPropString = ModifyOnAccessDenied<string>(value); } } 

     public static volatile bool _hasAccess = false; 
     private T ModifyOnAccessDenied<T>(T propertyToChange) 
     { 
      if (!_hasAccess) 
      { 
       if (propertyToChange is string) 
        return (dynamic)string.Empty; 
       else if (propertyToChange is int) 
        return (dynamic)(int)-1; 
      } 
      return propertyToChange; 
     } 
    } 
}