限制属性的字符串长度 [英] Limiting a String length on a property

查看:56
本文介绍了限制属性的字符串长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图找出一个更大的问题时出现了这个问题,为了简单起见,我省略了这个问题.

This question arose when I was trying to figure out a larger problem that, for simplicity sake, I'm omitting.

我必须在 C# 中表示某种数据结构.它是一种用于与外部系统通信的协议.因此,它具有一系列具有预定义长度和整数(或其他更复杂的数据)的字符串.我们假设:

I have to represent a certain data structure in C#. Its a protocol that will be used to communicate with an external system. As such, it has a sequence of strings with predefined lengths and integer (or other, more complicated data). Let's assume:

SYSTEM : four chars
APPLICATION : eight chars
ID : four-byte integer

现在,我首选的表示方式是使用字符串,所以

Now, my preferred way to represent this would be using strings, so

class Message
{
    string System {get; set; };      // four characters only!
    string Application {get; set; }; // eight chars
    int Id {get; set; };
}

问题是:我必须确保字符串的长度不超过预定义的长度.此外,这个标题实际上有十分之一的字段,这些字段会时不时地改变(我们仍在决定消息布局).

Problem is: I have to ensure that string doesn't have more than the predefined length. Furthermore, this header will actually have tenths of fields, are those will change every now and then (we are still deciding the message layout).

描述这种结构的最佳方式是什么?例如,我想使用带有数据描述的 XML 并使用反射来创建一个符合实现的类(因为我需要以编程方式访问它).

How is the best way to describe such structure? I thought, for example, to use a XML with the data description and use reflection in order to create a class that adheres to the implementation (since I need to access it programatically).

而且,就像我说的,还有更多的麻烦.我有其他类型的数据类型限制字符/数字的数量...

And, like I said, there is more trouble. I have other types of data types that limits the number of characters/digits...

推荐答案

对于初学者:整个长度问题.这很容易解决,不使用自动属性,而是声明自己的字段并以老式"方式编写属性.然后,您可以在 setter 中验证您的要求,并在新值无效时抛出异常或丢弃该值.

For starters: the whole length issue. That's easily solved by not using auto-properties, but instead declaring your own field and writing the property the "old-fashioned" way. You can then validate your requirement in the setter, and throw an exception or discard the new value if it's invalid.

对于不断变化的结构:如果无法进入并更改类,您可以编写一个解决方案,该解决方案使用字典(好吧,可能是您要存储的每种数据类型一个)将名称与值相关联.添加某种文件(可能是 XML),其中描述了允许的字段、它们的类型和验证要求.

For the changing structure: If it's not possible to just go in and alter the class, you could write a solution which uses a Dictionary (well, perhaps one per data type you want to store) to associate a name with a value. Add a file of some sort (perhaps XML) which describes the fields allowed, their type, and validation requirements.

然而,如果它只是因为你还没有决定最终结构而改变,我可能更喜欢只改变类 - 如果你在部署应用程序时不需要那种动态结构,看起来像浪费时间,因为您最终可能会花更多时间编写动态内容而不是更改类.

However, if it's just changing because you haven't decided on a final structure yet, I would probably prefer just changing the class - if you don't need that sort of dynamic structure when you deploy your application, it seems like a waste of time, since you'll probably end up spending more time writing the dynamic stuff than you would altering the class.

这篇关于限制属性的字符串长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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