C#:你在哪里可以把字符串转换/翻译功能? [英] C#: Where would you put to-and-from-string conversion/translation-functionality?

查看:163
本文介绍了C#:你在哪里可以把字符串转换/翻译功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些相当简单(至少当你跳过错误检查,事件填充等等)参数类时。它们是组装的,如下所示:

  public interface IParameter 
{
Type ValueType {get ; }
object Value {get;组; }
}

public interface IParameter< TValue> :IParameter
{
new TValue Value {get;组; }
}

public abstract class ParameterBase< TValue> :IParameter< TValue>
{
public abstract TValue Value {get;组; }
对象IParameter.Value
{
get {return Value; }
set
{
if(value is TValue)Value =(TValue)value;
else throw new ArgumentException(Wrong type。);
}
}
public Type ValueType {get {return typeof(TValue);因此,例如创建一个基于整数的参数,您将创建以下内容:
}

class:

  public class NumberParameter:ParameterBase< int> 
{
private int number;
public override int Value {get {return number; } set {number = value; }}
}

我的问题是,其中一些参数将具有文本表示形式用户界面。例如,值类型可以是整数列表,但用户应该能够将这些整数输入到文本框中,例如用逗号分隔。



其中你会把这个翻译/转换功能?你会如何实现它?有没有一个聪明的方法来使它整洁的代码?



目前我添加了一个公共字符串ValueAsText 属性对于需要这种转换的参数,但是我有点觉得它将参数类与功能截然不同,它绝对不需要关心。我在想一些中间类,类型转换器,格式化程序+解析器,或...某事...也许只是在这里说话乱七八糟,我有参数和文本框。我可以创建一个格式化器和一个解析器,用于将值转换为字符串和从字符串转换。但是如何以良好的方式连接点?



任何人明白我的意思?任何聪明的想法?






澄清:我不想将参数本身转换为字符串,只显示其值作为字符串,并从字符串设置值。换句话说,参数实例本身必须保持不变。






更多的说明:在ui中,参数是由通常由具有参数名称的标签,文本框和/或其他一些输入控件组成的组件表示。例如,date参数将具有一个文本框(您可以在其中键入日期范围)和一个打开日期选择器的按钮(您可以在其中选择日期范围)。在这种情况下,TValue的参数例如是 IEnumerable< DateTime> ,这是所有参数应该真正需要的。但是,由于我也希望用户能够以文本框中的字符串形式输入日期范围,所以我需要将该字符串转换成某个日期的序列。目前,我已经通过将ValueAsText属性添加到来回执行的date参数中。然后将TextBox与ValueAsText属性通话,并将日期选择器与属性通话。这有效,但我觉得参数类做的太多了。我认为翻译工作属于那里。但不知道挂钩的位置以及如何工作。例如,我可以有一个范围解析器类,可以将字符串解析成一个日期序列,一个范围格式化程序类可以占用一个范围并从中创建一个字符串。但谁来创造他们,他们住在哪里?我正在考虑一步可能只是创建这些类,并让他们生活在ValueAsText属性后面。这样大部分的功能至少是分开的。但是,我真的不知道。我正在寻找想法,想学习。这是这个问题的原因:)

解决方案

您是否想过实现 System.ComponentModel.TypeConverter 实现为您 NumberParameter 键入类,使它们成为.NET世界中一流的公民,如内置类型?这样,您不仅可以处理/从 string (包括例如 PropertyGrid 控件中的支持)的转换,以编辑NumberParameter '没有额外的工作,我认为这样的值的数据绑定到 TextBox.Text 等),但也可能的其他类型的转换/转换?


I have some fairly simple (at least when you skip error checking, event stuff, et cetera) parameter classes. They are "assembled" kind of like this:

public interface IParameter
{
    Type ValueType { get; }
    object Value { get; set; }
}

public interface IParameter<TValue> : IParameter
{
    new TValue Value { get; set; }
}

public abstract class ParameterBase<TValue> : IParameter<TValue>
{
    public abstract TValue Value { get; set; }
    object IParameter.Value
    {
        get { return Value; }
        set
        {
            if (value is TValue) Value = (TValue)value;
            else throw new ArgumentException("Wrong type.");
        }
    }
    public Type ValueType { get { return typeof(TValue); } }
}

So to for example create an integer based parameter you would create the following class:

public class NumberParameter : ParameterBase<int>
{
    private int number;
    public override int Value { get { return number; } set {number = value; } }
}

My issue is that some of these parameters will have a text representation in the user interface. For example the value type could be a list of integers, but the user should be able to type those integers in to a text box, for example separated by commas.

Where would you put this translation/conversion functionality? And how would you implement it? Is there a smart way to make it neat code-wise?

Currently I have added a public string ValueAsText property to the parameters that needs this conversion, but I kind of feel that it clutters up the parameter class with functionality it strictly shouldn't have to care about. I'm thinking some sort of intermediate class, type converter, formatter + parser, or... something... maybe just talking gibberish here... I have the parameter and the text box. I can create a formatter and a parser for converting the value to and from a string. But how do I connect the dots in a good way?

Anyone understand what I mean? Any clever ideas?


Clarification: I don't want to convert the parameter itself to a string, only display its value as a string and set the value from a string. In other words, the parameter instance itself have to stay the same.


More clarification: In the ui, the parameter is represented by a component that generally consist of a label with the parameter name, a textbox and/or some other input control. For example a date parameter would have a textbox (where you can type in a date range) and a button that opens a date-picker (where you can select a date range). In that case the parameters TValue would for example be IEnumerable<DateTime>, which is all that parameter should really need. But because I also want the user to be able to type that range of dates in as a string in a textbox, I need to convert that string into a sequence of dates somewhere. Currently I have done that by adding a ValueAsText property to the date parameter which does the translating back and forth. The TextBox is then "talks" to the ValueAsText property, and the date picker "talks" to the Value property. This works, but I kind of feel that the parameter classes are doing too much. I think that translating work belongs somewhere in between there. But not sure where and how the hooking up would then work. For example I could have a range parser class that can parse a string into a sequence of dates and a range formatter class that can take a range and create a string out of it. But who would create them and where would they live? I'm thinking step one could be to just create those classes and have them live behind the ValueAsText property. That way most of the functionality is separated out at least. But yeah, I don't really know. I'm looking for ideas and want to learn. That was the reason for this question :)

解决方案

Have you thought of implementing System.ComponentModel.TypeConverter implementations for you NumberParameter type classes, making them first-class citizens in the .NET world like built in types? That way you not only handle conversions to/from string (including for instance support in PropertyGrid controls for editing a 'NumberParameter' without extra work, and I think things like databinding of such values to TextBox.Text and the like) but also possible other type conversions/casts?

这篇关于C#:你在哪里可以把字符串转换/翻译功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆