~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/ServiceStack.Text/src/ServiceStack.Text/Common/StaticParseMethod.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// http://code.google.com/p/servicestack/wiki/TypeSerializer
 
3
// ServiceStack.Text: .NET C# POCO Type Text Serializer.
 
4
//
 
5
// Authors:
 
6
//   Demis Bellot (demis.bellot@gmail.com)
 
7
//
 
8
// Copyright 2011 Liquidbit Ltd.
 
9
//
 
10
// Licensed under the same terms of ServiceStack: new BSD license.
 
11
//
 
12
 
 
13
using System;
 
14
using System.Reflection;
 
15
 
 
16
namespace ServiceStack.Text.Common
 
17
{
 
18
        internal delegate object ParseDelegate(string value);
 
19
 
 
20
        public static class StaticParseMethod<T>
 
21
        {
 
22
                const string ParseMethod = "Parse";
 
23
 
 
24
                private static readonly ParseStringDelegate CacheFn;
 
25
 
 
26
                public static ParseStringDelegate Parse
 
27
                {
 
28
                        get { return CacheFn; }
 
29
                }
 
30
 
 
31
                static StaticParseMethod()
 
32
                {
 
33
                        CacheFn = GetParseFn();
 
34
                }
 
35
 
 
36
                public static ParseStringDelegate GetParseFn()
 
37
                {
 
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);
 
42
 
 
43
                        if (parseMethodInfo == null) return null;
 
44
 
 
45
                        ParseDelegate parseDelegate;
 
46
                        try
 
47
                        {
 
48
                                parseDelegate = (ParseDelegate)Delegate.CreateDelegate(typeof(ParseDelegate), parseMethodInfo);
 
49
                        }
 
50
                        catch ( ArgumentException )
 
51
                        {
 
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);
 
55
                        }
 
56
                        if (parseDelegate != null)
 
57
                                return value => parseDelegate(value.FromCsvField());
 
58
 
 
59
                        return null;
 
60
                }
 
61
 
 
62
        }
 
63
}
 
 
b'\\ No newline at end of file'