de4dot-cex/AssemblyData/methodsrewriter/MType.cs

101 lines
3.1 KiB
C#
Raw Permalink Normal View History

2011-09-27 07:48:47 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2011-09-27 07:48:47 +08:00
This file is part of de4dot.
de4dot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
de4dot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Reflection;
using dnlib.DotNet;
2011-09-27 07:48:47 +08:00
using de4dot.blocks;
namespace AssemblyData.methodsrewriter {
class MType {
public Type type;
2012-11-01 05:44:54 +08:00
public TypeDef typeDef;
2011-09-27 07:48:47 +08:00
Dictionary<int, MMethod> tokenToMethod;
2012-11-22 16:14:51 +08:00
MethodDefDict<MMethod> methodRefToMethod;
2011-09-27 07:48:47 +08:00
Dictionary<int, MField> tokenToField;
2012-11-22 16:14:51 +08:00
FieldDefDict<MField> fieldRefToField;
2011-09-27 07:48:47 +08:00
2012-11-22 16:14:51 +08:00
public MType(Type type, TypeDef typeDef) {
2011-09-27 07:48:47 +08:00
this.type = type;
2012-11-22 16:14:51 +08:00
this.typeDef = typeDef;
2011-09-27 07:48:47 +08:00
}
2013-01-19 20:03:57 +08:00
public MMethod GetMethod(IMethod methodRef) {
InitMethods();
return methodRefToMethod.Find(methodRef);
2011-09-27 07:48:47 +08:00
}
2013-01-19 20:03:57 +08:00
public MField GetField(IField fieldRef) {
InitFields();
return fieldRefToField.Find(fieldRef);
2011-09-27 07:48:47 +08:00
}
2013-01-19 20:03:57 +08:00
public MMethod GetMethod(int token) {
InitMethods();
2011-09-27 07:48:47 +08:00
return tokenToMethod[token];
}
2013-01-19 20:03:57 +08:00
public MField GetField(int token) {
InitFields();
2011-09-27 07:48:47 +08:00
return tokenToField[token];
}
2013-01-19 20:03:57 +08:00
void InitMethods() {
2011-09-27 07:48:47 +08:00
if (tokenToMethod != null)
return;
2012-11-01 05:44:54 +08:00
tokenToMethod = new Dictionary<int, MMethod>(typeDef.Methods.Count);
2012-11-22 16:14:51 +08:00
methodRefToMethod = new MethodDefDict<MMethod>();
2011-09-27 07:48:47 +08:00
var tmpTokenToMethod = new Dictionary<int, MethodBase>();
var flags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
2013-01-19 20:03:57 +08:00
foreach (var m in ResolverUtils.GetMethodBases(type, flags))
2011-09-27 07:48:47 +08:00
tmpTokenToMethod[m.MetadataToken] = m;
2012-11-01 05:44:54 +08:00
foreach (var m in typeDef.Methods) {
var token = (int)m.MDToken.Raw;
2011-09-27 07:48:47 +08:00
var method = new MMethod(tmpTokenToMethod[token], m);
tokenToMethod[token] = method;
2013-01-19 20:03:57 +08:00
methodRefToMethod.Add(method.methodDef, method);
2011-09-27 07:48:47 +08:00
}
}
2013-01-19 20:03:57 +08:00
void InitFields() {
2011-09-27 07:48:47 +08:00
if (tokenToField != null)
return;
2012-11-01 05:44:54 +08:00
tokenToField = new Dictionary<int, MField>(typeDef.Fields.Count);
2012-11-22 16:14:51 +08:00
fieldRefToField = new FieldDefDict<MField>();
2011-09-27 07:48:47 +08:00
var tmpTokenToField = new Dictionary<int, FieldInfo>();
var flags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
foreach (var f in type.GetFields(flags))
tmpTokenToField[f.MetadataToken] = f;
2012-11-01 05:44:54 +08:00
foreach (var f in typeDef.Fields) {
var token = (int)f.MDToken.Raw;
2011-09-27 07:48:47 +08:00
var field = new MField(tmpTokenToField[token], f);
tokenToField[token] = field;
2013-01-19 20:03:57 +08:00
fieldRefToField.Add(field.fieldDef, field);
2011-09-27 07:48:47 +08:00
}
}
public override string ToString() {
2012-11-01 05:44:54 +08:00
return string.Format("{0:X8} - {1}", typeDef.MDToken.Raw, typeDef.FullName);
2011-09-27 07:48:47 +08:00
}
}
}