将文件命名模式存储(和使用)到 class 属性
Storing (and using) file naming schemas to a class property
我有一个包含 stateProfile
class 和 Inquiry
class 的项目。
Inquiry
class 是静态的,它的工作是创建一个文本文件。文本文件的内容是使用 stateProfile
对象属性中存储的信息计算的(美国每个州一个,最终从 XML 文件中读取)。
我已经能够为所有州共享一个全局命名模式,但我现在需要满足基于 stateProfile
对象命名它们的要求。
例如:
我有一个堪萨斯州的 stateProfile
和密苏里州的另一个。
Inquiry inquiry = new Inquiry();
foreach(stateProfile p in _listOfProfiles){
inquiry.CreateFile(p);
}
in Inquiry.CreateFile(stateProfile p)
我有定义文件名的表达式:
sOutFileName = $"{p.prop1}_{p.prop2}_Literal3_{_staticProp.ToString("D4")}.txt";
关于能够将 logic/expression 存储在 属性 中的任何建议,例如 stateProfile.outFileName
例如
public string outFileName {
$"{p.prop1}_{p.prop2}_Literal3_{_staticProp.ToString("D4")}.txt";
}
然后能够像下面这样在 Inquiry.CreateFile(stateProfile p)
中引用那个 属性?
sOutFileName = $"{p.outFileName}";
我认为这种方法可能正是您正在寻找的。它允许您将 C# 脚本嵌入到您的 XML 中并在代码中解释它们。
现在每个州都可以有不同的文件名格式。
using System;
using Microsoft.CodeAnalysis.Scripting;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static async Task Main(string[] args)
{
// This was read from your XML file.
var filenameFormatterScript = "p => $\"{p.Name.ToLower()}.txt\"";
var options = ScriptOptions.Default.AddReferences(typeof(StateProfile).Assembly);
var sp = new StateProfile
{
Name = "Alabama",
FilenameFormatter = await CSharpScript.EvaluateAsync<Func<StateProfile, string>>(filenameFormatterScript, options)
};
Console.WriteLine(sp.Filename());
}
}
public class StateProfile
{
public string Name { get; set; }
public Func<StateProfile, string> FilenameFormatter { get; set; }
public string Filename()
{
return FilenameFormatter(this);
}
}
}
再考虑一下后, 是正确的选择。
因为我希望在构建文件名公式时使用的所有属性都属于 stateProfile
对象,所以我创建了一个方法 GetFileName()
class 成员并传入任何内容否则我可能需要。
我使用 switch 语句,随着我必须考虑的状态数量的增加,它可能会变得笨拙,但现在它可以工作。
public string GetFileName(int prop1) {
string sOutFileName = "";
switch (SourceState) {
case "MO":
sOutFileName = $"ReqPref_{this.stProfPropA}_{prop1}.txt";
break;
default:
sOutFileName = $"{this.stProfPropB}_{prop1}.txt";
break;
}
return sOutFileName;
}
我有一个包含 stateProfile
class 和 Inquiry
class 的项目。
Inquiry
class 是静态的,它的工作是创建一个文本文件。文本文件的内容是使用 stateProfile
对象属性中存储的信息计算的(美国每个州一个,最终从 XML 文件中读取)。
我已经能够为所有州共享一个全局命名模式,但我现在需要满足基于 stateProfile
对象命名它们的要求。
例如:
我有一个堪萨斯州的 stateProfile
和密苏里州的另一个。
Inquiry inquiry = new Inquiry();
foreach(stateProfile p in _listOfProfiles){
inquiry.CreateFile(p);
}
in Inquiry.CreateFile(stateProfile p)
我有定义文件名的表达式:
sOutFileName = $"{p.prop1}_{p.prop2}_Literal3_{_staticProp.ToString("D4")}.txt";
关于能够将 logic/expression 存储在 属性 中的任何建议,例如 stateProfile.outFileName
例如
public string outFileName {
$"{p.prop1}_{p.prop2}_Literal3_{_staticProp.ToString("D4")}.txt";
}
然后能够像下面这样在 Inquiry.CreateFile(stateProfile p)
中引用那个 属性?
sOutFileName = $"{p.outFileName}";
我认为这种方法可能正是您正在寻找的。它允许您将 C# 脚本嵌入到您的 XML 中并在代码中解释它们。
现在每个州都可以有不同的文件名格式。
using System;
using Microsoft.CodeAnalysis.Scripting;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static async Task Main(string[] args)
{
// This was read from your XML file.
var filenameFormatterScript = "p => $\"{p.Name.ToLower()}.txt\"";
var options = ScriptOptions.Default.AddReferences(typeof(StateProfile).Assembly);
var sp = new StateProfile
{
Name = "Alabama",
FilenameFormatter = await CSharpScript.EvaluateAsync<Func<StateProfile, string>>(filenameFormatterScript, options)
};
Console.WriteLine(sp.Filename());
}
}
public class StateProfile
{
public string Name { get; set; }
public Func<StateProfile, string> FilenameFormatter { get; set; }
public string Filename()
{
return FilenameFormatter(this);
}
}
}
再考虑一下后,
因为我希望在构建文件名公式时使用的所有属性都属于 stateProfile
对象,所以我创建了一个方法 GetFileName()
class 成员并传入任何内容否则我可能需要。
我使用 switch 语句,随着我必须考虑的状态数量的增加,它可能会变得笨拙,但现在它可以工作。
public string GetFileName(int prop1) {
string sOutFileName = "";
switch (SourceState) {
case "MO":
sOutFileName = $"ReqPref_{this.stProfPropA}_{prop1}.txt";
break;
default:
sOutFileName = $"{this.stProfPropB}_{prop1}.txt";
break;
}
return sOutFileName;
}