Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.
C#
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.
C#
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.
C#
public class Response
{
       public Uri Uri { get; set; }
       public decimal Price { get; set; }
}


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.
C#
Response.Uri = matchUri.Parse<Response.Uri.GetType()>();
Posted
Comments
CodeHawkz 6-May-11 7:04am    
Try,
Uri = matchUri.Parse<typeof(response.uri)>();
Homer77 6-May-11 13:43pm    
That wont compile either :-(

1 solution

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:

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


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:

C#
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> 
{ /*...*/ }


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
 
Share this answer
 
v3
Comments
Homer77 6-May-11 14:21pm    
Thanks for your answer SAKryukov.
I see your point on the generic interface, but changing the interface moves the problem to the factory.CreatePatternMatch instead. The answer is that my design is flawed since Generic arguments must be resolvable at compile time.


IPatternMatch matchUri = PatternMatchFactory.CreatePatternMatch<Pattern.Type.GetType()>();
Sergey Alexandrovich Kryukov 6-May-11 14:47pm    
The last line (or anything simular) won't compile no matter what you invent.
Please listen to a good friendly advice: describe more general idea and use cases on a higher level, may be there is something to think about.
--SA
Homer77 6-May-11 15:37pm    
The goal is to build a assembly to extract different pieces of data from a string of html. The pieces can be a title, url, price or number. The assembly will be fed with a collection of Pattern objects specifying the regular expression and the data type the match has to be converted to. A regex will return a string but i would like an extension on top of that to verify (tryparse) that the extracted string is in fact convertible to the exspected data type. This is where the factory comes into play. It will provide the concrete type of the regex matcher class and verify that the extracted data can i fact be converted. If it can, the data has to be extracted from a method or property which dont know its own return type at compile time.
I hope it cleared things up.
And yes, the last line or similar will never compile as stated in my last comment.
Albin Abel 7-May-11 17:18pm    
sure. My 5
Sergey Alexandrovich Kryukov 7-May-11 20:04pm    
Thank you, Albin.
It requires some further consideration. Nothing fancy goes here.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900