-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormActivator.cs
More file actions
62 lines (51 loc) · 1.99 KB
/
FormActivator.cs
File metadata and controls
62 lines (51 loc) · 1.99 KB
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
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Phoenix.Core;
using Phoenix.Extentions;
namespace Phoenix
{
internal static class FormActivator
{
internal static Dictionary<string, FormConstructorDelegate> _formConstructorDelegates
= new Dictionary<string, FormConstructorDelegate>();
internal static void AddFormConstructor(string name, FormConstructorDelegate constructorDelegate)
{
_formConstructorDelegates.Add(name, constructorDelegate);
}
internal delegate object FormConstructorDelegate();
internal static FormConstructorDelegate CreateFormConstructor(Type type, params Type[] parameters)
{
var constructorInfo = type.GetConstructor(parameters);
var body = Expression.New(constructorInfo);
var constructor = Expression.Lambda<FormConstructorDelegate>(body);
return constructor.Compile();
}
internal static void TryActivateForm<T>(string formName) where T : PhoenixForm
{
if (!PContainer.CheckExistsForm(formName))
{
var formConstructor = _formConstructorDelegates.Get(formName);
PhoenixForm form = formConstructor() as PhoenixForm;
ActivateForm(form);
_formConstructorDelegates.Remove(formName);
}
}
internal static void ActivateForm(PhoenixForm form)
{
PContainer.Append(form.Name, form);
form.InitializeEvents();
form.EnableFormHiding();
}
internal static void ProtectsAndValidatesPassedTypes(Type formType)
{
if (!formType.IsClass || formType.BaseType.Name != typeof(PhoenixForm).Name)
{
throw new PhoenixException(
"The passed argument is not a type of the PhoenixForm class!",
new ArgumentException()
);
}
}
}
}