2 * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
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.
19 using System.Reflection;
25 public class Converter
27 private AssemblyDefinition _asm;
29 public Converter(string srcAssemblyPath)
31 _asm = AssemblyDefinition.ReadAssembly(srcAssemblyPath);
34 public void ConvertTo(string targetAssemblyPath)
37 _asm.Write(targetAssemblyPath);
40 private void VisitAssembly(AssemblyDefinition assembly)
42 Log.Verbose("Assembly: " + assembly.FullName);
44 var attr = assembly.CustomAttributes.FirstOrDefault(a => a.AttributeType.FullName == "System.Runtime.CompilerServices.ReferenceAssemblyAttribute");
46 assembly.CustomAttributes.Remove(attr);
49 foreach (var module in assembly.Modules)
55 private void VisitModule(ModuleDefinition module)
57 Log.Verbose("Module: " + module.Name);
58 foreach (var type in module.Types)
64 private void VisitType(TypeDefinition type)
66 Log.Verbose("Type: " + type.FullName);
68 foreach (var prop in type.Properties)
73 foreach (var method in type.Methods)
78 foreach (var ev in type.Events)
83 foreach (var nested in type.NestedTypes)
89 private void VisitProperty(PropertyDefinition prop)
91 Log.Verbose(" Property: " + prop.FullName);
92 if (prop.GetMethod != null)
94 Log.Verbose(" Getter:");
95 ReplaceWithThrowPNSE(prop.GetMethod);
97 if (prop.SetMethod != null)
99 Log.Verbose(" Setter:");
100 ReplaceWithThrowPNSE(prop.SetMethod);
104 private void VisitMethod(MethodDefinition method)
106 Log.Verbose($" Method: {method.FullName}");
107 if (method.Name == "Finalize")
109 ReplaceWithVoidReturn(method);
113 ReplaceWithThrowPNSE(method);
117 private void VisitEvent(EventDefinition ev)
119 Log.Verbose(" Event: " + ev.FullName);
120 if (ev.AddMethod != null)
122 Log.Verbose(" Add:");
123 ReplaceWithThrowPNSE(ev.AddMethod);
125 if (ev.RemoveMethod != null)
127 Log.Verbose(" Remove:");
128 ReplaceWithThrowPNSE(ev.RemoveMethod);
130 foreach (var method in ev.OtherMethods)
132 Log.Verbose(" Other: " + method.FullName);
133 ReplaceWithThrowPNSE(method);
137 private bool ReplaceWithThrowPNSE(MethodDefinition method)
139 if (method.HasBody && method.IsIL)
141 ClearMethodBody(method);
143 ConstructorInfo exceptionCtor = typeof(PlatformNotSupportedException).GetConstructor(new Type[] { typeof(System.String) });
144 var exceptionCtorRef = method.Module.ImportReference(exceptionCtor);
146 var processor = method.Body.GetILProcessor();
147 processor.Append(processor.Create(OpCodes.Ldstr, "Not Supported Feature"));
148 processor.Append(processor.Create(OpCodes.Newobj, exceptionCtorRef));
149 processor.Append(processor.Create(OpCodes.Throw));
151 PrintMethodBody(method);
158 private bool ReplaceWithVoidReturn(MethodDefinition method)
160 if (method.HasBody && method.IsIL)
162 ClearMethodBody(method);
164 var processor = method.Body.GetILProcessor();
165 processor.Append(processor.Create(OpCodes.Nop));
166 processor.Append(processor.Create(OpCodes.Ret));
168 PrintMethodBody(method);
175 private void ClearMethodBody(MethodDefinition method)
177 var processor = method.Body.GetILProcessor();
178 while (method.Body.Instructions.Count > 0)
180 processor.Remove(method.Body.Instructions[0]);
184 private void PrintMethodBody(MethodDefinition method)
188 var iLProcessor = method.Body.GetILProcessor();
189 foreach (var inst in method.Body.Instructions)
191 Log.Verbose($" {inst.ToString()}");