C# 将静态类用作另一个类的输入参数
我不知道这是否可能,但感觉应该是从使用C#到现在为止 我希望有一组静态类,其中包含“设置值”,供库用户作为参数发送到另一个类中 这就是我要去的地方,但我想不出来。下面这只是我所想的一个例子,所以不要试图找出“为什么”:) 第一个-将被调用的类C# 将静态类用作另一个类的输入参数,c#,class,parameter-passing,C#,Class,Parameter Passing,我不知道这是否可能,但感觉应该是从使用C#到现在为止 我希望有一组静态类,其中包含“设置值”,供库用户作为参数发送到另一个类中 这就是我要去的地方,但我想不出来。下面这只是我所想的一个例子,所以不要试图找出“为什么”:) 第一个-将被调用的类 public class myClass { public bool isError { private set; get; } public DataTable output { private set; get
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(),这不是我必须要做的事情,但当我在这里的时候…:-基本上没有;但你可以这样做:
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
作为实例传递。否。这是不可能的。这是不可能的,原因有二:
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 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中,静态类在允许您执行的操作方面非常有限
但是,使用反射和类型
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
};
}
参数必须是实例。静态类没有实例。所有这些静态类和值基本上都是一个巨大的问题,您没有设计好的面向对象程序。仅从外观上看,
fileTypeA
、fileTypeB
和fileTypeC
应该是从同一个类派生的实例化类。不要懒惰,使用懒惰!)@ErikPhilips无法解析;我不知道你在暗示什么,或者为什么这会是一件好事,完全搞乱了你。我通常使用private static Lazy(..)\u FileTypeA;公共静态{get;}=\u FileTypeA.value但我的意思是,在这种情况下,这完全是过火了。这正是我想要的工作方式!在本例中,主类上的参数是什么样的?公共设置模块设置{get;set;};