簡體   English   中英

使用靜態類作為另一個類的輸入參數

[英]Using a static class as an input parameter on another class

我不知道這是否可能,但感覺應該是從使用C#到目前為止。

我希望有一堆靜態類,包含“set values”,供庫的用戶作為參數發送到另一個類。

所以這就是我的目標,但我無法弄明白。 下面這只是我的想法的一個例子所以不要試圖找出'為什么':-)

第一個 - 將被調用的類

public class myClass
    {
        public bool isError { private set; get; }

        public DataTable output { private set; get; }

        public String filename { set; private get; }

        public settingModule settings { set; private get; }

        public static void execute()
        {


            //Call Private 'getTheData'
            //set isError accordingly
            //Load output


        }

        private static DataTable getTheData()
        {
            //Open and read file for 'fileName'

            //Use settings.startingRow
            //Use settings.fileType
            //User settings.skipEmpty

            //Do some stuff

            return Datatable from workings

        }



    }

第二 - 我希望用戶傳遞的類

public static class settingMobule
    {
        public static class fileTypeA
        {
            public static int startingRow = 1;
            public static String fileType = "txt";
            public static bool skipEmpty = true;
        }

        public static class fileTypeB
        {
            public static int startingRow = 10;
            public static String fileType = "csv";
            public static bool skipEmpty = false;
        }

        public static class fileTypeC
        {
            public static int startingRow = 3;
            public static String fileType = "hex";
            public static bool skipEmpty = true;
        }

    }

最后我希望能夠調用它的方式

myClass test = new myClass();

test.filename = "c:\\temp\\test.txt;

test.settings = settingModule.fileTypeA;

test.execute();

if(test.isError == false

{
DataTable myTable = test.output;
test.dispose()
}

在此先感謝...是的,“你的堅果有一個更好的方法”是一個非常有效的答案:-)

我也很想知道如何在我的代碼中添加.dispose(),這不是我必須要做的事情,但是當我在這里時...... :-D

不,基本上; 但你可以這樣做:

public sealed class SettingMobule
{
    public int StartingRow {get; private set;}
    public string FileType {get; private set;}
    public bool SkipEmpty {get; private set;}

    private SettingMobule(int startingRow, string fileType, bool skipEmpty)
    {
        StartingRow = startingRow;
        FileType = fileType;
        SkipEmpty = skipEmpty;
    }
    public static SettingMobule FileTypeA {get;}
          = new SettingMobule(1, "txt", true);

    public static SettingMobule FileTypeB {get;}
          = new SettingMobule(10, "csv", false);

    public static SettingMobule FileTypeC {get;}
          = new SettingMobule(3, "hex", true);

}

並將SettingMobule.FileTypeA作為實例傳遞等。

不,這是不可能的。 由於兩個原因,這是不可能的:

  1. 靜態類不能傳遞。

  2. 接收方無法知道這些類應該包含相同的設置集,並且無法訪問它們。

選擇另一種方法,其中只有一個非靜態文件類型類用於創建多個設置對象:(C#6.0)

public class FileType
{
    public FileType(int startingRow, string extension, bool skipEmpty)
    {
        this.StartingRow = startingRow;
        this.Extension = extension;  // 'FileType': Member names cannot be the same as their
                                     // enclosing type.
        this.SkipEmpty = skipEmpty;
    }

    public int StartingRow { get; }
    public string Extension { get; }
    public bool SkipEmpty { get; }
}

靜態設置類現在可以呈現幾個可以傳遞的相同類型的設置對象。

public static class SettingModule
{
    public static FileType TxtFileType { get; } = new FileType(1, "txt", true);
    public static FileType CsvFileType { get; } = new FileType(10, "csv", false);
    public static FileType HexFileType { get; } = new FileType(3, "hex", true);
}

現在,測試類可以寫成:

public class MyTestClass
{
    private FileType fileType;
    private string filename;

    public MyTestClass(FileType fileType, string filename)
    {
        this.fileType = fileType;
        this.filename = filename;
    }

    public void Execute()
    {
        Console.WriteLine(
            $"Extension = {fileType.Extension}, starting row = {fileType.StartingRow}");
    }
}

你可以像這樣進行測試

var test = new MyTestClass(SettingModule.TxtFileType, @"c:\temp\test.txt");
test.Execute();

非靜態類是一種模板,可以從中創建大量對象。 與靜態類不同,此類是可用於聲明變量,方法參數,屬性等的類型。

不幸的是,在C#靜態類中,它們允許你做的事情非常有限。

但是,使用Reflection和Type s,你可以做類似的事情,但我認為你不應該這樣做。

void Main() {
    var test = new MyClass(typeof(settingModule.fileTypeB));
    Console.WriteLine(test.StartingRow);
}

public class MyClass {
    Type SettingsClass { get; set; }

    public MyClass(Type sc) {
        SettingsClass = sc;
    }

    public int StartingRow {
        get {
            return (int)SettingsClass.GetField("startingRow", BindingFlags.Static | BindingFlags.Public).GetValue(null);
        }
    }
}

public static class settingModule {
    public static class fileTypeA {
        public static int startingRow = 1;
        public static String fileType = "txt";
        public static bool skipEmpty = true;
    }

    public static class fileTypeB {
        public static int startingRow = 10;
        public static String fileType = "csv";
        public static bool skipEmpty = false;
    }

    public static class fileTypeC {
        public static int startingRow = 3;
        public static String fileType = "hex";
        public static bool skipEmpty = true;
    }

}

我認為你應該做的是創建一個子類的實例並傳遞:

void Main() {
    var test = new MyClass();
    test.Settings = settingModule.fileTypeA;
    Console.WriteLine(test.Settings.startingRow);
}

public class MyClass {
    public settingModule.settingsSet Settings { get; set; }

}

public static class settingModule {
    public class settingsSet {
        public readonly int startingRow;
        public readonly string fileType;
        public readonly bool skipEmpty;
        public settingsSet(int sr, string ft, bool se) {
            startingRow = sr;
            fileType = ft;
            skipEmpty = se;
        }
    }

    public static settingsSet fileTypeA = new settingsSet(1, "txt", true);
    public static settingsSet fileTypeB = new settingsSet(10, "csv", false);
    public static settingsSet fileTypeC = new settingsSet(3, "hex", true);
}

您甚至可以將其編寫為更像靜態類:

public static class settingModule {
    public struct settingsSet {
        public int startingRow;
        public string fileType;
        public bool skipEmpty;
    }

    public static readonly settingsSet fileTypeA = new settingsSet {
        startingRow = 1,
        fileType = "txt",
        skipEmpty = true
    };

    public static readonly settingsSet fileTypeB = new settingsSet {
        startingRow = 10,
        fileType = "csv",
        skipEmpty = false
    };

    public static readonly settingsSet fileTypeC = new settingsSet {
        startingRow = 3,
        fileType = "hex",
        skipEmpty = true
    };
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM