Subversion Repository Public Repository

WOX2

@ 14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.IO;
using System.Reflection;
using System.Text;

/// <header>
/// <wox>
///     <version>1.0</version>
///     <author>Carlos R. Jaimez Gonzalez</author>
///     <author>Simon M. Lucas</author>
///     <site>http://woxserializer.sourceforge.net/</site>
/// </wox>
/// <wox>
///     <version>1.5</version>
///     <author>Steven M Lewis</author>
/// </wox>
/// <wox>
///     <version>2.0</version>
///     <author>George A Smith</author>
///     <svn>http://woxserializer.sourceforge.net/</svn>
///     <note>XML form for vs 2 is more compact and therefor incompatible with vs 1</note>
/// </wox>
/// </header>

namespace wox.serial
{
    /// <summary>
    /// The Util class provides static methods that are used by SimpleReader and SimpleWriter.
    /// The methods of this class are used by the serialization and de-serialization processes.
    /// </summary>

    public class Util
    {
        public static void Dump( Exception pException, TextWriter pWriter )
        {
            string zPrefix = "";
            for ( int i = 0; i < 99; i++ )
            {
                zPrefix += pException.GetType().ToString();
                pWriter.WriteLine( zPrefix + ": " + pException.Message );
                pWriter.WriteLine( pException.StackTrace );
                Exception zInnerException = pException.InnerException;
                if ( (zInnerException == null) || (zInnerException == pException) )
                {
                    return;
                }
                zPrefix = "Caused By: ";
                pException = zInnerException;
            }
        }

        public static ConstructorInfo ForceDefaultConstructor( Type cl )
        {
            ConstructorInfo defaultConstructor = cl.GetConstructor( Type.EmptyTypes );
            if ( defaultConstructor == null )
            {
                throw new ArgumentException( "Cannot find default constructor for type " + cl );
            }
            return defaultConstructor;
        }

        public static bool IsNullable( Type typeOb )
        {
            String typeName = typeOb.Name;
            if ( typeName.StartsWith( "Nullable`1" ) )
            {
                if ( typeName == "Nullable`1" )
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Turn a String into a Type
        /// </summary>
        /// <param name="pTypeName">!null type name</param>
        public static Type ResolveType( String pTypeName )
        {
            // well try the normal way
            Type zType = Type.GetType( pTypeName );

            if ( zType == null )
            {
                //try getting the type from the assembly that call this one...
                Assembly zAssembly = Assembly.GetEntryAssembly();
                if ( zAssembly != null )
                {
                    zType = zAssembly.GetType( pTypeName );
                }

                // well try the runtime environment
                if ( zType == null )
                {
                    Assembly[] appAssemblies = AppDomain.CurrentDomain.GetAssemblies();
                    foreach ( Assembly zAppAssembly in appAssemblies )
                    {
                        if ( null != (zType = zAppAssembly.GetType( pTypeName )) )
                        {
                            return zType;
                        }
                    }
                }
            }
            return zType;
        }

        public static String BuildCannotResolveMessage( String typeName )
        {
            var sb = new StringBuilder();
            sb.Append( "Cannot resolve type " + typeName );
            sb.Append( "\n" );

            sb.Append( "Looked in Assemblies:" );
            sb.Append( "\n" );
            Assembly[] appAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            for ( int i = 0; i < appAssemblies.Length; i++ )
            {
                Assembly assembly = appAssemblies[i];
                String name = assembly.GetName().Name;
                sb.Append( name );
                sb.Append( "\n" );
            }


            return sb.ToString();
        }

        public static object CreateInstance( Type pType, string pWoxTypeName )
        {
            Object zInstance = Activator.CreateInstance( pType );

            if ( zInstance != null )
            {
                return zInstance;
            }
            string zPlus = (pWoxTypeName == null) ? ": " + pType : " (" + pType + ") for Wox Type: " + pWoxTypeName;
            throw new ApplicationException( "Unable to instantiate instance from Native Type" + zPlus );
        }
    }
}

Commits for WOX2/trunk/CSharp/src/wox/serial/Util.cs

Diff revisions: vs.
Revision Author Commited Message
14 Diff Diff GeorgeS picture GeorgeS Fri 19 Feb, 2010 19:15:03 +0000
3 GeorgeS picture GeorgeS Thu 04 Feb, 2010 23:53:47 +0000