~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Main/Base/Project/Src/Gui/XmlForms/Lib/DefaultObjectCreator.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Drawing;
 
6
using System.Reflection;
 
7
using System.Windows.Forms;
 
8
using System.Xml;
 
9
 
 
10
namespace ICSharpCode.SharpDevelop.Gui.XmlForms 
 
11
{
 
12
        /// <summary>
 
13
        /// Default implementation of the IObjectCreator interface.
 
14
        /// </summary>
 
15
        public class DefaultObjectCreator : IObjectCreator
 
16
        {
 
17
                public virtual Type GetType(string name)
 
18
                {
 
19
                        Type t = typeof(Control).Assembly.GetType(name);
 
20
                        
 
21
                        // try to create System.Drawing.* objects
 
22
                        if (t == null) {
 
23
                                t = typeof(Point).Assembly.GetType(name);
 
24
                        }
 
25
                        
 
26
                        // try to create System.* objects
 
27
                        if (t == null) {
 
28
                                t = typeof(String).Assembly.GetType(name);
 
29
                        }
 
30
                        
 
31
                        // try to create the object from some assembly which is currently
 
32
                        // loaded by the running application.
 
33
                        if (t == null) {
 
34
                                Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
 
35
                                
 
36
                                foreach (Assembly assembly in assemblies) {
 
37
                                        t = assembly.GetType(name);
 
38
                                        if (t != null) {
 
39
                                                break;
 
40
                                        }
 
41
                                }
 
42
                        }
 
43
                        
 
44
                        return t;
 
45
                }
 
46
                public virtual object CreateObject(string name, XmlElement el)
 
47
                {
 
48
                        try {
 
49
                                // try to create System.Windows.Forms.* objects
 
50
                                object newObject = typeof(Control).Assembly.CreateInstance(name);
 
51
                                
 
52
                                // try to create System.Drawing.* objects
 
53
                                if (newObject == null) {
 
54
                                        newObject = typeof(Point).Assembly.CreateInstance(name);
 
55
                                }
 
56
                                
 
57
                                // try to create System.* objects
 
58
                                if (newObject == null) {
 
59
                                        newObject = typeof(String).Assembly.CreateInstance(name);
 
60
                                }
 
61
                                
 
62
                                // try to create the object from some assembly which is currently
 
63
                                // loaded by the running application.
 
64
                                if (newObject == null) {
 
65
                                        Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
 
66
                                        
 
67
                                        foreach (Assembly assembly in assemblies) {
 
68
                                                newObject = assembly.CreateInstance(name);
 
69
                                                if (newObject != null) {
 
70
                                                        break;
 
71
                                                }
 
72
                                        }
 
73
                                }
 
74
                                
 
75
                                if (newObject is Control) {
 
76
                                        ((Control)newObject).SuspendLayout();
 
77
                                }
 
78
                                
 
79
                                return newObject;                       
 
80
                        } catch (Exception) {
 
81
                                return null;
 
82
                        }
 
83
                }
 
84
        }
 
85
}