2
// http://code.google.com/p/servicestack/wiki/TypeSerializer
3
// ServiceStack.Text: .NET C# POCO Type Text Serializer.
6
// Demis Bellot (demis.bellot@gmail.com)
8
// Copyright 2011 Liquidbit Ltd.
10
// Licensed under the same terms of ServiceStack: new BSD license.
14
using System.Reflection;
16
namespace ServiceStack.Text.Common
18
internal delegate object ParseDelegate(string value);
20
public static class StaticParseMethod<T>
22
const string ParseMethod = "Parse";
24
private static readonly ParseStringDelegate CacheFn;
26
public static ParseStringDelegate Parse
28
get { return CacheFn; }
31
static StaticParseMethod()
33
CacheFn = GetParseFn();
36
public static ParseStringDelegate GetParseFn()
38
// Get the static Parse(string) method on the type supplied
39
var parseMethodInfo = typeof(T).GetMethod(
40
ParseMethod, BindingFlags.Public | BindingFlags.Static, null,
41
new[] { typeof(string) }, null);
43
if (parseMethodInfo == null) return null;
45
ParseDelegate parseDelegate;
48
parseDelegate = (ParseDelegate)Delegate.CreateDelegate(typeof(ParseDelegate), parseMethodInfo);
50
catch ( ArgumentException )
52
//Try wrapping strongly-typed return with wrapper fn.
53
var typedParseDelegate = (Func<string,T>)Delegate.CreateDelegate(typeof(Func<string,T>), parseMethodInfo);
54
parseDelegate = x => typedParseDelegate(x);
56
if (parseDelegate != null)
57
return value => parseDelegate(value.FromCsvField());
b'\\ No newline at end of file'