2 * Copyright(c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
20 using System.Reflection;
23 using MethodAttributes = Mono.Cecil.MethodAttributes;
24 using MethodImplAttributes = Mono.Cecil.MethodImplAttributes;
26 namespace Tizen.NUI.Xaml.Build.Tasks
28 static class TypeDefinitionExtensions
30 public static MethodDefinition AddDefaultConstructor(this TypeDefinition targetType)
32 var module = targetType.Module;
33 var parentType = module.ImportReference(("mscorlib", "System", "Object"));
35 return AddDefaultConstructor(targetType, parentType);
38 public static MethodDefinition AddDefaultConstructor(this TypeDefinition targetType, TypeReference parentType)
40 var module = targetType.Module;
41 var voidType = module.ImportReference(("mscorlib", "System", "Void"));
42 var methodAttributes = MethodAttributes.Public |
43 MethodAttributes.HideBySig |
44 MethodAttributes.SpecialName |
45 MethodAttributes.RTSpecialName;
47 var parentctor = module.ImportCtorReference(parentType, paramCount: 0) ?? module.ImportCtorReference(("mscorlib", "System", "Object"), parameterTypes: null);
49 var ctor = new MethodDefinition(".ctor", methodAttributes, voidType)
51 CallingConvention = MethodCallingConvention.Default,
52 ImplAttributes = (MethodImplAttributes.IL | MethodImplAttributes.Managed)
54 ctor.Body.InitLocals = true;
56 var IL = ctor.Body.GetILProcessor();
58 IL.Emit(OpCodes.Ldarg_0);
59 IL.Emit(OpCodes.Call, parentctor);
62 targetType.Methods.Add(ctor);
66 public static IEnumerable<MethodDefinition> AllMethods(this TypeDefinition self)
70 foreach (var md in self.Methods)
72 self = self.BaseType == null ? null : self.BaseType.ResolveCached();
76 public static FieldDefinition GetOrCreateField(this TypeDefinition self, string name, Mono.Cecil.FieldAttributes attributes, TypeReference fieldType)
78 var field = self.Fields.FirstOrDefault(a => a.Name == name);
82 field = new FieldDefinition(name, attributes, fieldType);
83 self.Fields.Add(field);
89 public static MethodDefinition GetOrCreateMethod(this TypeDefinition self, string name, MethodAttributes attributes, Type type)
91 MethodDefinition method = self.Methods.FirstOrDefault(a => a.Name == name);
94 method = new MethodDefinition(name, MethodAttributes.Public, self.Module.ImportReference(type));
95 self.Methods.Add(method);