动态类型转换 [英] Dynamic type conversion

查看:80
本文介绍了动态类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助.
我使用多个正则表达式编写一些代码以匹配字符串(html)的不同部分.每个表达式返回不同类型的数据ex.小数和uri.
每个表达式都由一个PatternMatchDecimal,PatternMatchUri类表示,并从基类PatternMatchBase继承,该基类将返回一个字符串,并且继承的类会将匹配项转换为它们各自的类型.
我已经为继承类PatternMatchDecimal和PatternMatchUri定义了一个接口.

Hi guys, i need some help on this.
Im writing some code to match different parts of a string (html) using multiple regular expressions. Each expression return different types of data ex. decimal and uri.
Each expression is represented by a class PatternMatchDecimal, PatternMatchUri and inherit from the base class PatternMatchBase which will return a string and the inheriting classes will convert the match to the their respective types.

Ive defined a interface for the inhereting classes PatternMatchDecimal and PatternMatchUri.

interface IPatternMatch
{
     bool Match(string html);
     T Parse<T>();
     object Result { get; }
     Type ResultType { get; }
}



我正在使用工厂从模式类型创建具体类型.



Im using a factory to create the concrete types from the type of pattern.

public class Pattern
{
     public string Regex { get; set; }
     public PatternType Type { get; set; }
}

IPatternMatch matchUri = PatternMatchFactory.CreatePatternMatch(Pattern);



结果数据将收集到响应对象中.



The resulting data is to be collected in a response object.

public class Response
{
       public Uri Uri { get; set; }
       public decimal Price { get; set; }
}



现在,我需要将正则表达式的IPatternMatch实现的对象Result转换为Response对象中的属性的类型.

像这样的东西,不会编译.



Now i need to convert the object Result from the IPatternMatch implementation of the regular expression to the type of the property in the Response object.

Something like this, which wont compile.

Response.Uri = matchUri.Parse<Response.Uri.GetType()>();

推荐答案

如果您真正实现了Parse,可以将其解析为任何类型(很难相信,但是假设是否对某些类型解析),我希望它能做到这一点:

If your really implement Parse the way it can parse into any type (which is hard to believe, but let''s assume if does it for some type(s)), I would expect it does this:

IPatternMatch matchUri = //...
Response.Uri = matchUri.Parse<system.uri>(); 
</system.uri>



也就是说,一般参数在编译时应该是静态已知的.我认为这违反了您设计的目的;这意味着您的整个设计是错误的,因为您对通用名称稍有误解,并将其与OOP late绑定混合使用(可以假定您查看的最后一条代码行将无法编译).

从Result属性的基本类型为Object并取决于System.Type ResultType的事实也可以看出这一点.如果您对这种类型使用通用方法,则整个接口应该是通用的:



That is, the generic parameter should be known statically at the moment of compilation. I think this defeats the purpose of your design; and it means your whole design is wrong as you slightly misunderstood generics and mix it with OOP late binding (which one can assume looking at your last code line which would not compile).

This can be also seen from the fact that you Result property is of the base type Object and depends on the System.Type ResultType. If you used generic approach for this type, whole interface should be generic:

interface IPatternMatch<T>
{
     bool Match(string html);
     T Parse();
     T Result { get; } //not really needed, I would assume
}

class IntegerMatch : IPatternMatch<int> 
{
     bool Match(string html) { return int.TryParse(html, out FResult); }
     int IPatternMatch<int>Parse(/* where is a string to parse? */) { /*...*/ }
     int IPatternMatch<int>Result { get { return FResult; /* when it's parsed */ } } 
     int FResult;
}

class UriMatch  : IPatternMatch<System.Uri> 
{ /*...*/ }



这个想法足以实现您想要的行为吗?
从我在代码中的注释中,您应该看到应该更改接口.

那行得通;而且我看不到您的设计如何运作.我不能解决"它,因为您没有解释您的最终目标或总体思路,因为您对想要什么"的解释被如何做"污染了.如果您解释了纯"的想法或要求,我也许可以提供合理的设计.

—SA



Is this idea enough for you to implement your desired behavior?
From my comments in the code you should see the interface should be changed.

That would work; and I don''t see how your design could work. I cannot "fix" it though, because you did not explain your ultimate goal or general idea, because your explanation of "what you want" was "contaminated" with "how to do it". If you explained the "pure" idea or requirements, I would probably be able to offer a reasonable design.

—SA


这篇关于动态类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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