-
Notifications
You must be signed in to change notification settings - Fork 219
/
VisualBasicSettings.cs
107 lines (91 loc) · 4.26 KB
/
VisualBasicSettings.cs
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
// This file is part of Core WF which is licensed under the MIT license.
// See LICENSE file in the project root for full license information.
using System;
using System.Activities;
using System.Activities.Internals;
using System.Activities.Runtime;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;
using System.Windows.Markup;
using System.Xaml;
using Microsoft.VisualBasic.Activities.XamlIntegration;
using Microsoft.VisualBasic.CompilerServices;
namespace Microsoft.VisualBasic.Activities;
using CompilerFactory = Func<HashSet<Assembly>, JustInTimeCompiler>;
[ValueSerializer(typeof(VisualBasicSettingsValueSerializer))]
[TypeConverter(typeof(VisualBasicSettingsConverter))]
public class VisualBasicSettings
{
private static readonly HashSet<VisualBasicImportReference> s_defaultImportReferences = new()
{
//"mscorlib"
new VisualBasicImportReference {Import = "System", Assembly = typeof(object).Assembly.FullName},
new VisualBasicImportReference {Import = "System.Collections", Assembly = "System.Runtime"},
new VisualBasicImportReference {Import = "System.Collections.Generic", Assembly = "System.Collections"},
//"system"
new VisualBasicImportReference
{Import = "System.ComponentModel", Assembly = typeof(BrowsableAttribute).Assembly.FullName},
new VisualBasicImportReference { Import = "System.Linq", Assembly = typeof(System.Linq.Enumerable).Assembly.FullName },
new VisualBasicImportReference
{Import = "System.Linq.Expressions", Assembly = typeof(Expression).Assembly.FullName},
new VisualBasicImportReference {Import = "System", Assembly = "system"},
new VisualBasicImportReference
{Import = "System.Collections.Generic", Assembly = typeof(HashSet<>).Assembly.FullName},
//"System.Activities"
new VisualBasicImportReference {Import = "System.Activities", Assembly = "System.Activities"},
new VisualBasicImportReference {Import = "System.Activities.Statements", Assembly = "System.Activities"},
new VisualBasicImportReference {Import = "System.Activities.Expressions", Assembly = "System.Activities"},
// Microsoft.VisualBasic
new VisualBasicImportReference
{Import = "Microsoft.VisualBasic", Assembly = typeof(Conversions).Assembly.FullName}
};
private CompilerFactory _compilerFactory = references => new VbJitCompiler(references);
public VisualBasicSettings()
{
ImportReferences = new HashSet<VisualBasicImportReference>();
}
private VisualBasicSettings(HashSet<VisualBasicImportReference> importReferences)
{
Fx.Assert(importReferences != null, "caller must verify");
ImportReferences = new HashSet<VisualBasicImportReference>(importReferences);
}
public static VisualBasicSettings Default { get; } = new(s_defaultImportReferences);
// hide from XAML since the value serializer can't suppress yet
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public ISet<VisualBasicImportReference> ImportReferences { get; }
public CompilerFactory CompilerFactory
{
get => _compilerFactory;
set
{
_compilerFactory = value;
CompilerChanged?.Invoke(this, EventArgs.Empty);
}
}
internal bool SuppressXamlSerialization { get; set; }
internal event EventHandler CompilerChanged;
internal static JustInTimeCompiler CreateCompiler(HashSet<Assembly> references)
{
return Default.CompilerFactory(references);
}
internal void GenerateXamlReferences(IValueSerializerContext context)
{
// promote settings to xmlns declarations
var namespaceLookup = GetService<INamespacePrefixLookup>(context);
foreach (var importReference in ImportReferences)
{
importReference.GenerateXamlNamespace(namespaceLookup);
}
}
internal static T GetService<T>(ITypeDescriptorContext context) where T : class
{
var service = (T) context.GetService(typeof(T));
if (service == null)
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.InvalidTypeConverterUsage));
}
return service;
}
}