我可以在 C# 中创建接口或抽象类来声明具有未知类型参数的方法吗?
我正在将 http GET 查询字符串解析为其组件。在尝试使其模块化(参数的数量和类型可能会有很大变化)时,我想要一个 Parameter 抽象基类或接口来定义属性是否已设置,以及一个设置属性的 Set 方法价值。有没有办法使用 Set 方法的可变参数类型来做到这一点?
总体思路如下:
public abstract class Parameter
{
public bool IsSet { get; protected set; }
protected Parameter() { IsSet = false; }
public abstract void Set( --unknown type here-- );
}
示例参数子项将类似于:
public class IntParameter : Parameter
{
public int Value { get; protected set; }
public void Set(int value)
{
Value = value;
IsSet = true;
}
}
通过这种结构,我可以将每个查询参数放入其适当的强类型类中,但仍然确保所有参数的行为一致。 IsSet 属性的原因是能够检查参数是否已设置,因为某些参数没有任何“安全”值,我确信这些值不是故意传递的。如果未设置值,则会使用默认值。
看看这个问题,我怀疑它是否可以按照我想要实现的方式进行处理,但是这个示例应该能够让我明白我想要做什么。
对于如何最好地处理它有什么建议吗?如果有一种方便的设计模式或通用的方法来实现它,我不会感到惊讶,但我还没有成功地在谷歌上搜索到一个。
我立即看到的选项是:
- 不要使用继承并依靠约定来保持一致性。我很可能不需要通过列表迭代等来处理这些,但如果我找到一种方法来做到这一点,它可能会带来一些新的想法或机会。
- 使用对象参数,然后执行一些 typeof() 并以某种方式切换魔法,尽管这让我觉得非常丑陋,而且可以说是非多态的。
还有其他想法吗? :)
I'm parsing a http GET query string into its components. In trying to make it modular (the number and types of parameters can vary quite a bit), I want to have a Parameter abstract base class or interface that defines whether a property has been set or not, along with a Set method that sets the value. Is there a way to do that with a variable parameter type for the Set method?
The general idea is as follows:
public abstract class Parameter
{
public bool IsSet { get; protected set; }
protected Parameter() { IsSet = false; }
public abstract void Set( --unknown type here-- );
}
A sample parameter child would then be something like:
public class IntParameter : Parameter
{
public int Value { get; protected set; }
public void Set(int value)
{
Value = value;
IsSet = true;
}
}
With this kind of structure I could then toss each query parameter into its appropriate strongly typed class, but still ensure that all of them are acting consistently. The reason for the IsSet property is to be able to check if the parameter has been set or not, since some parameters don't have any 'safe' values that I would know for sure weren't intentionally passed. If a value is not set, then it would get a default value stuck into it instead.
Looking at the problem, I doubt it can be handled as I want to implement it, but the example should get the idea across about what I'd like to be able to do.
Are there any suggestions on how to best handle it. I wouldn't be surprised if there were a handy design pattern or common way of doing it, but I haven't managed to Google one up.
The options I see off hand are:
- Don't use inheritance and rely on convention instead for consistency. I most likely wouldn't need to process these through list iterations or such, though if I found a way to do it, it might open some new ideas or opportunities.
- Use an object parameter and then do some typeof() and switch magic in some way, though that strikes me as very ugly, and non-polymorphic, so to speak.
Any other ideas? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否正在寻找类似的内容
请查看简介到 C# 泛型
Are you maybe looking for something like this
Have a look at An Introduction to C# Generics