0047749d26ae3c6b9e8927713c4f5fabd04b78f9
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.XamlBuild / src / public / XamlBuild / XamlCAssemblyResolver.cs
1 /*
2  * Copyright(c) 2022 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 using System;
18 using Mono.Cecil;
19
20 namespace Tizen.NUI.Xaml.Build.Tasks
21 {
22     class XamlCAssemblyResolver : DefaultAssemblyResolver
23     {
24         public void AddAssembly(string p)
25         {
26             RegisterAssembly(AssemblyDefinition.ReadAssembly(p, new ReaderParameters
27             {
28                 AssemblyResolver = this
29             }));
30         }
31
32         public override AssemblyDefinition Resolve(AssemblyNameReference name)
33         {
34             if (TryResolve(name, out AssemblyDefinition assembly))
35                 return assembly;
36             if (   IsMscorlib(name)
37                 && (  TryResolve(AssemblyNameReference.Parse("mscorlib"), out assembly)
38                    || TryResolve(AssemblyNameReference.Parse("netstandard"), out assembly)
39                    || TryResolve(AssemblyNameReference.Parse("System.Runtime"), out assembly)))
40                 return assembly;
41             throw new AssemblyResolutionException(name);
42         }
43
44         bool TryResolve(AssemblyNameReference assemblyNameReference, out AssemblyDefinition assembly)
45         {
46             try {
47                 assembly = base.Resolve(assemblyNameReference);
48                 return true;
49             }
50             catch (AssemblyResolutionException e) {
51                 assembly = null;
52                 return false;
53             }
54         }
55
56         static bool IsMscorlib(AssemblyNameReference name)
57         {
58             return    name.Name == "mscorlib"
59                    || name.Name == "System.Runtime"
60                    || name.Name == "netstandard";
61         }
62     }
63 }
64