2016-07-27 72 views
0

我想構造函數調用只允許有限範圍的「擴展」。比方說,我有這2類:與其他類常數值調用構造函數

public class Foo 
{ 
    public Foo(Extension ext) 
    { 
     // do something 
    } 
} 

public class Extension 
{ 
    public const string TXT = ".txt"; 
    public const string XML = ".xml"; 
} 

所以,當其他開發人員希望使用Foo他只能用值從Extension類這樣做,像這樣:當試圖

Foo foo = new Foo(Extension.TXT); 

但要做到這一點我得到一個IDE錯誤說:"cannot convert from 'string' to '<ProjectName>.Extension'

作爲「解決方法」我能我Extension類更改爲這樣的事情:

public class Extension 
{ 
    public enum File 
    { 
     TXT, 
     XML 
    } 
} 

,並使用它像這樣:

Foo foo = new Foo(Extension.File.TXT); 

這工作完全正常,但我不喜歡這個調用是一個更長的級別(class - > enum - > element而不是class - > element)。

所以,問題是我的解決方法,實際上是唯一有效,正確或最佳實踐解決方案?

+3

從類中刪除NUM並使它上市。你只會調用enum:'new Foo(File.Txt);'。 –

回答

5

可以使用的Java風格枚舉

public class Extension 
{ 
    string _Extension = null; 

    private Extension(string ext) 
    { 
     _Extension = ext; 
    } 

    public static Extension TXT 
    { 
     get { return new Extension(".txt"); } 
    } 

    public static Extension XML 
    { 
     get { return new Extension(".xml"); } 
    } 

    public override string ToString() 
    { 
     return _Extension; 
    } 

    public override bool Equals(object obj) 
    { 
     var e = obj as Extension; 
     if (e == null) return false; 

     return e._Extension == this._Extension; 
    } 

    public override int GetHashCode() 
    { 
     return _Extension.GetHashCode(); 
    } 
} 
3

第一個示例具有Extension被用作具有幾個字符串常量的類。第二個例子使用枚舉來代替常量。

public class Foo 
{ 
    // this .ctor expects a type of Extension, not a string. 
    public Foo(Extension ext) 
    { 
     // do something 
    } 
} 

// This class has only constant string values. 
public class Extension 
{ 
    public const string TXT = ".txt"; 
    public const string XML = ".xml"; 
} 

試圖在一個字符串傳遞給上述.ctor因爲它期待一個類型的Extension,而不是一個串將不工作。

// this passes in a string, not a type. 
Foo foo = new Foo(Extension.TXT); 

當你是想限制提供給Foo .ctor的值,然後使用一個枚舉,你有你的第二個例子:

public class Foo 
{ 
    public Foo(File ext) 
    { 
     // do something 
    } 
} 

public enum File 
{ 
    TXT, 
    XML 
} 

然後按預期這將工作:

Foo foo = new Foo(File.TXT); 
0

您可以爲字符串定義一個默認構造函數和隱式運算符爲Extension。例如。是這樣的:

public class Extension 
{ 
    public const string TXT = ".txt"; 
    public const string XML = ".xml"; 

    private _value; 
    public Extension(string value){if(value == TXT || value == XML) _value = value; else throw new NotImplementedException();} 
    public static implicit operator string(Extension value){return _value;} 
    public static implicit operator Extension(string value){if(value == TXT || value == XML) _value = value; else throw new NotImplementedException();} 
} 

這樣你可以調用foo( 「TXT」)或富(Extension.TXT)

,或者你可以定義TXT爲擴展名的一個實例:

public class Extension 
{ 
    public const Extension TXT = new Extension(".txt"); 
    public const Extension XML = new Extension(".xml"); 

    public Value{get;private set;} 
    public Extension(string value){Value = value;} 
} 
1

爲什麼不脫在Foo類之外的clare enum並且沒有像擴展那樣的特殊類?

public enum Extension 
{ 
    TXT, 
    XML 
} 

public class Foo 
{ 
    public Foo(Extension ext) 
    { 
     // do something 
    } 
} 

然後,當你正在構建一個Foo對象,你可以簡單地做:

Foo foo = new Foo(Extension.TXT); 
0

剛剛從類改變Extesion的第一個聲明來枚舉

相關問題