Add type, method, field, prop, event dictionaries

This commit is contained in:
de4dot 2011-12-16 18:46:30 +01:00
parent ca2a756a0e
commit f30b0ef749
5 changed files with 375 additions and 111 deletions

View File

@ -27,7 +27,7 @@ namespace AssemblyData.methodsrewriter {
class MModule { class MModule {
public Module module; public Module module;
public ModuleDefinition moduleDefinition; public ModuleDefinition moduleDefinition;
Dictionary<TypeReferenceKey, MType> typeReferenceToType = new Dictionary<TypeReferenceKey, MType>(); TypeDefinitionDict<MType> typeReferenceToType = new TypeDefinitionDict<MType>();
Dictionary<int, MType> tokenToType = new Dictionary<int, MType>(); Dictionary<int, MType> tokenToType = new Dictionary<int, MType>();
Dictionary<int, MMethod> tokenToGlobalMethod; Dictionary<int, MMethod> tokenToGlobalMethod;
Dictionary<int, MField> tokenToGlobalField; Dictionary<int, MField> tokenToGlobalField;
@ -52,14 +52,12 @@ namespace AssemblyData.methodsrewriter {
foreach (var token in tmpTokenToType.Keys) { foreach (var token in tmpTokenToType.Keys) {
var mtype = new MType(tmpTokenToType[token], tmpTokenToTypeDefinition[token]); var mtype = new MType(tmpTokenToType[token], tmpTokenToTypeDefinition[token]);
tokenToType[token] = mtype; tokenToType[token] = mtype;
typeReferenceToType[new TypeReferenceKey(mtype.typeDefinition)] = mtype; typeReferenceToType.add(mtype.typeDefinition, mtype);
} }
} }
public MType getType(TypeReference typeReference) { public MType getType(TypeReference typeReference) {
MType type; return typeReferenceToType.find(typeReference);
typeReferenceToType.TryGetValue(new TypeReferenceKey(typeReference), out type);
return type;
} }
public MMethod getMethod(MethodReference methodReference) { public MMethod getMethod(MethodReference methodReference) {

View File

@ -28,9 +28,9 @@ namespace AssemblyData.methodsrewriter {
public Type type; public Type type;
public TypeDefinition typeDefinition; public TypeDefinition typeDefinition;
Dictionary<int, MMethod> tokenToMethod; Dictionary<int, MMethod> tokenToMethod;
Dictionary<MethodReferenceKey, MMethod> methodReferenceToMethod; MethodDefinitionDict<MMethod> methodReferenceToMethod;
Dictionary<int, MField> tokenToField; Dictionary<int, MField> tokenToField;
Dictionary<FieldReferenceKey, MField> fieldReferenceToField; FieldDefinitionDict<MField> fieldReferenceToField;
public MType(Type type, TypeDefinition typeDefinition) { public MType(Type type, TypeDefinition typeDefinition) {
this.type = type; this.type = type;
@ -39,16 +39,12 @@ namespace AssemblyData.methodsrewriter {
public MMethod getMethod(MethodReference methodReference) { public MMethod getMethod(MethodReference methodReference) {
initMethods(); initMethods();
MMethod method; return methodReferenceToMethod.find(methodReference);
methodReferenceToMethod.TryGetValue(new MethodReferenceKey(methodReference), out method);
return method;
} }
public MField getField(FieldReference fieldReference) { public MField getField(FieldReference fieldReference) {
initFields(); initFields();
MField field; return fieldReferenceToField.find(fieldReference);
fieldReferenceToField.TryGetValue(new FieldReferenceKey(fieldReference), out field);
return field;
} }
public MMethod getMethod(int token) { public MMethod getMethod(int token) {
@ -65,7 +61,7 @@ namespace AssemblyData.methodsrewriter {
if (tokenToMethod != null) if (tokenToMethod != null)
return; return;
tokenToMethod = new Dictionary<int, MMethod>(typeDefinition.Methods.Count); tokenToMethod = new Dictionary<int, MMethod>(typeDefinition.Methods.Count);
methodReferenceToMethod = new Dictionary<MethodReferenceKey, MMethod>(); methodReferenceToMethod = new MethodDefinitionDict<MMethod>();
var tmpTokenToMethod = new Dictionary<int, MethodBase>(); var tmpTokenToMethod = new Dictionary<int, MethodBase>();
var flags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; var flags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
@ -75,7 +71,7 @@ namespace AssemblyData.methodsrewriter {
var token = m.MetadataToken.ToInt32(); var token = m.MetadataToken.ToInt32();
var method = new MMethod(tmpTokenToMethod[token], m); var method = new MMethod(tmpTokenToMethod[token], m);
tokenToMethod[token] = method; tokenToMethod[token] = method;
methodReferenceToMethod[new MethodReferenceKey(method.methodDefinition)] = method; methodReferenceToMethod.add(method.methodDefinition, method);
} }
} }
@ -83,7 +79,7 @@ namespace AssemblyData.methodsrewriter {
if (tokenToField != null) if (tokenToField != null)
return; return;
tokenToField = new Dictionary<int, MField>(typeDefinition.Fields.Count); tokenToField = new Dictionary<int, MField>(typeDefinition.Fields.Count);
fieldReferenceToField = new Dictionary<FieldReferenceKey, MField>(); fieldReferenceToField = new FieldDefinitionDict<MField>();
var tmpTokenToField = new Dictionary<int, FieldInfo>(); var tmpTokenToField = new Dictionary<int, FieldInfo>();
var flags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; var flags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
@ -93,7 +89,7 @@ namespace AssemblyData.methodsrewriter {
var token = f.MetadataToken.ToInt32(); var token = f.MetadataToken.ToInt32();
var field = new MField(tmpTokenToField[token], f); var field = new MField(tmpTokenToField[token], f);
tokenToField[token] = field; tokenToField[token] = field;
fieldReferenceToField[new FieldReferenceKey(field.fieldDefinition)] = field; fieldReferenceToField.add(field.fieldDefinition, field);
} }
} }

View File

@ -25,7 +25,7 @@ using Mono.Cecil.Cil;
namespace de4dot.blocks { namespace de4dot.blocks {
class TypeCache { class TypeCache {
ModuleDefinition module; ModuleDefinition module;
Dictionary<TypeReferenceKey, TypeDefinition> typeRefToDef = new Dictionary<TypeReferenceKey, TypeDefinition>(); TypeDefinitionDict<TypeDefinition> typeRefToDef = new TypeDefinitionDict<TypeDefinition>();
public TypeCache(ModuleDefinition module) { public TypeCache(ModuleDefinition module) {
this.module = module; this.module = module;
@ -33,16 +33,12 @@ namespace de4dot.blocks {
} }
void init() { void init() {
foreach (var type in module.GetTypes()) { foreach (var type in module.GetTypes())
var key = new TypeReferenceKey(type); typeRefToDef.add(type, type);
typeRefToDef[key] = type;
}
} }
public TypeDefinition lookup(TypeReference typeReference) { public TypeDefinition lookup(TypeReference typeReference) {
TypeDefinition typeDefinition; return typeRefToDef.find(typeReference);
typeRefToDef.TryGetValue(new TypeReferenceKey(typeReference), out typeDefinition);
return typeDefinition;
} }
} }

View File

@ -44,6 +44,256 @@ namespace de4dot.blocks {
TypeReference, TypeReference,
} }
public class TypeDefinitionDict<TValue> {
Dictionary<ScopeAndTokenKey, TValue> tokenToValue = new Dictionary<ScopeAndTokenKey, TValue>();
Dictionary<TypeReferenceKey, TValue> refToValue = new Dictionary<TypeReferenceKey, TValue>();
public int Count {
get { return tokenToValue.Count; }
}
public IEnumerable<TValue> getAll() {
return tokenToValue.Values;
}
ScopeAndTokenKey getTokenKey(TypeReference typeReference) {
return new ScopeAndTokenKey(typeReference);
}
TypeReferenceKey getReferenceKey(TypeReference typeReference) {
return new TypeReferenceKey(typeReference);
}
public TValue find(TypeReference typeReference) {
TValue value;
if (tokenToValue.TryGetValue(getTokenKey(typeReference), out value))
return value;
refToValue.TryGetValue(getReferenceKey(typeReference), out value);
return value;
}
public void add(TypeDefinition typeDefinition, TValue value) {
tokenToValue[getTokenKey(typeDefinition)] = value;
refToValue[getReferenceKey(typeDefinition)] = value;
}
public void onTypesRenamed() {
var newTypeRefToValue = new Dictionary<TypeReferenceKey, TValue>(refToValue.Count);
foreach (var kvp in refToValue)
newTypeRefToValue[getReferenceKey((TypeDefinition)kvp.Key.TypeReference)] = kvp.Value;
refToValue = newTypeRefToValue;
}
}
public abstract class FieldDefinitionDictBase<TValue> {
Dictionary<ScopeAndTokenKey, TValue> tokenToValue = new Dictionary<ScopeAndTokenKey, TValue>();
Dictionary<IFieldReferenceKey, TValue> refToValue = new Dictionary<IFieldReferenceKey, TValue>();
public int Count {
get { return tokenToValue.Count; }
}
public IEnumerable<TValue> getAll() {
return tokenToValue.Values;
}
ScopeAndTokenKey getTokenKey(FieldReference fieldReference) {
return new ScopeAndTokenKey(fieldReference);
}
protected abstract IFieldReferenceKey getReferenceKey(FieldReference fieldReference);
public TValue find(FieldReference fieldReference) {
TValue value;
if (tokenToValue.TryGetValue(getTokenKey(fieldReference), out value))
return value;
refToValue.TryGetValue(getReferenceKey(fieldReference), out value);
return value;
}
public void add(FieldDefinition fieldDefinition, TValue value) {
tokenToValue[getTokenKey(fieldDefinition)] = value;
refToValue[getReferenceKey(fieldDefinition)] = value;
}
public void onTypesRenamed() {
var newFieldRefToDef = new Dictionary<IFieldReferenceKey, TValue>(refToValue.Count);
foreach (var kvp in refToValue)
newFieldRefToDef[getReferenceKey((FieldDefinition)kvp.Key.FieldReference)] = kvp.Value;
refToValue = newFieldRefToDef;
}
}
public class FieldDefinitionDict<TValue> : FieldDefinitionDictBase<TValue> {
protected override IFieldReferenceKey getReferenceKey(FieldReference fieldReference) {
return new FieldReferenceKey(fieldReference);
}
}
public class FieldDefinitionAndDeclaringTypeDict<TValue> : FieldDefinitionDictBase<TValue> {
protected override IFieldReferenceKey getReferenceKey(FieldReference fieldReference) {
return new FieldReferenceAndDeclaringTypeKey(fieldReference);
}
}
public abstract class MethodDefinitionDictBase<TValue> {
Dictionary<ScopeAndTokenKey, TValue> tokenToValue = new Dictionary<ScopeAndTokenKey, TValue>();
Dictionary<IMethodReferenceKey, TValue> refToValue = new Dictionary<IMethodReferenceKey, TValue>();
public int Count {
get { return tokenToValue.Count; }
}
public IEnumerable<TValue> getAll() {
return tokenToValue.Values;
}
ScopeAndTokenKey getTokenKey(MethodReference methodReference) {
return new ScopeAndTokenKey(methodReference);
}
protected abstract IMethodReferenceKey getReferenceKey(MethodReference methodReference);
public TValue find(MethodReference methodReference) {
TValue value;
if (tokenToValue.TryGetValue(getTokenKey(methodReference), out value))
return value;
refToValue.TryGetValue(getReferenceKey(methodReference), out value);
return value;
}
public void add(MethodDefinition methodDefinition, TValue value) {
tokenToValue[getTokenKey(methodDefinition)] = value;
refToValue[getReferenceKey(methodDefinition)] = value;
}
public void onTypesRenamed() {
var newFieldRefToDef = new Dictionary<IMethodReferenceKey, TValue>(refToValue.Count);
foreach (var kvp in refToValue)
newFieldRefToDef[getReferenceKey((MethodDefinition)kvp.Key.MethodReference)] = kvp.Value;
refToValue = newFieldRefToDef;
}
}
public class MethodDefinitionDict<TValue> : MethodDefinitionDictBase<TValue> {
protected override IMethodReferenceKey getReferenceKey(MethodReference methodReference) {
return new MethodReferenceKey(methodReference);
}
}
public class MethodDefinitionAndDeclaringTypeDict<TValue> : MethodDefinitionDictBase<TValue> {
protected override IMethodReferenceKey getReferenceKey(MethodReference methodReference) {
return new MethodReferenceAndDeclaringTypeKey(methodReference);
}
}
public abstract class PropertyDefinitionDictBase<TValue> {
Dictionary<ScopeAndTokenKey, TValue> tokenToValue = new Dictionary<ScopeAndTokenKey, TValue>();
Dictionary<IPropertyReferenceKey, TValue> refToValue = new Dictionary<IPropertyReferenceKey, TValue>();
public int Count {
get { return tokenToValue.Count; }
}
public IEnumerable<TValue> getAll() {
return tokenToValue.Values;
}
ScopeAndTokenKey getTokenKey(PropertyReference propertyReference) {
return new ScopeAndTokenKey(propertyReference);
}
protected abstract IPropertyReferenceKey getReferenceKey(PropertyReference propertyReference);
public TValue find(PropertyReference propertyReference) {
TValue value;
if (tokenToValue.TryGetValue(getTokenKey(propertyReference), out value))
return value;
refToValue.TryGetValue(getReferenceKey(propertyReference), out value);
return value;
}
public void add(PropertyDefinition propertyDefinition, TValue value) {
tokenToValue[getTokenKey(propertyDefinition)] = value;
refToValue[getReferenceKey(propertyDefinition)] = value;
}
public void onTypesRenamed() {
var newFieldRefToDef = new Dictionary<IPropertyReferenceKey, TValue>(refToValue.Count);
foreach (var kvp in refToValue)
newFieldRefToDef[getReferenceKey((PropertyDefinition)kvp.Key.PropertyReference)] = kvp.Value;
refToValue = newFieldRefToDef;
}
}
public class PropertyDefinitionDict<TValue> : PropertyDefinitionDictBase<TValue> {
protected override IPropertyReferenceKey getReferenceKey(PropertyReference propertyReference) {
return new PropertyReferenceKey(propertyReference);
}
}
public class PropertyDefinitionAndDeclaringTypeDict<TValue> : PropertyDefinitionDictBase<TValue> {
protected override IPropertyReferenceKey getReferenceKey(PropertyReference propertyReference) {
return new PropertyReferenceAndDeclaringTypeKey(propertyReference);
}
}
public abstract class EventDefinitionDictBase<TValue> {
Dictionary<ScopeAndTokenKey, TValue> tokenToValue = new Dictionary<ScopeAndTokenKey, TValue>();
Dictionary<IEventReferenceKey, TValue> refToValue = new Dictionary<IEventReferenceKey, TValue>();
public int Count {
get { return tokenToValue.Count; }
}
public IEnumerable<TValue> getAll() {
return tokenToValue.Values;
}
ScopeAndTokenKey getTokenKey(EventReference eventReference) {
return new ScopeAndTokenKey(eventReference);
}
protected abstract IEventReferenceKey getReferenceKey(EventReference eventReference);
public TValue find(EventReference eventReference) {
TValue value;
if (tokenToValue.TryGetValue(getTokenKey(eventReference), out value))
return value;
refToValue.TryGetValue(getReferenceKey(eventReference), out value);
return value;
}
public void add(EventDefinition eventDefinition, TValue value) {
tokenToValue[getTokenKey(eventDefinition)] = value;
refToValue[getReferenceKey(eventDefinition)] = value;
}
public void onTypesRenamed() {
var newFieldRefToDef = new Dictionary<IEventReferenceKey, TValue>(refToValue.Count);
foreach (var kvp in refToValue)
newFieldRefToDef[getReferenceKey((EventDefinition)kvp.Key.EventReference)] = kvp.Value;
refToValue = newFieldRefToDef;
}
}
public class EventDefinitionDict<TValue> : EventDefinitionDictBase<TValue> {
protected override IEventReferenceKey getReferenceKey(EventReference eventReference) {
return new EventReferenceKey(eventReference);
}
}
public class EventDefinitionAndDeclaringTypeDict<TValue> : EventDefinitionDictBase<TValue> {
protected override IEventReferenceKey getReferenceKey(EventReference eventReference) {
return new EventReferenceAndDeclaringTypeKey(eventReference);
}
}
public class ScopeAndTokenKey { public class ScopeAndTokenKey {
readonly IMetadataScope scope; readonly IMetadataScope scope;
readonly int token; readonly int token;
@ -144,7 +394,23 @@ namespace de4dot.blocks {
} }
} }
public class FieldReferenceKey { public interface IFieldReferenceKey {
FieldReference FieldReference { get; }
}
public interface IPropertyReferenceKey {
PropertyReference PropertyReference { get; }
}
public interface IEventReferenceKey {
EventReference EventReference { get; }
}
public interface IMethodReferenceKey {
MethodReference MethodReference { get; }
}
public class FieldReferenceKey : IFieldReferenceKey {
readonly FieldReference fieldRef; readonly FieldReference fieldRef;
public FieldReference FieldReference { public FieldReference FieldReference {
@ -171,7 +437,7 @@ namespace de4dot.blocks {
} }
} }
public class PropertyReferenceKey { public class PropertyReferenceKey : IPropertyReferenceKey {
readonly PropertyReference propRef; readonly PropertyReference propRef;
public PropertyReference PropertyReference { public PropertyReference PropertyReference {
@ -198,7 +464,7 @@ namespace de4dot.blocks {
} }
} }
public class EventReferenceKey { public class EventReferenceKey : IEventReferenceKey {
readonly EventReference eventRef; readonly EventReference eventRef;
public EventReference EventReference { public EventReference EventReference {
@ -225,7 +491,7 @@ namespace de4dot.blocks {
} }
} }
public class MethodReferenceKey { public class MethodReferenceKey : IMethodReferenceKey {
readonly MethodReference methodRef; readonly MethodReference methodRef;
public MethodReference MethodReference { public MethodReference MethodReference {
@ -252,7 +518,7 @@ namespace de4dot.blocks {
} }
} }
public class FieldReferenceAndDeclaringTypeKey { public class FieldReferenceAndDeclaringTypeKey : IFieldReferenceKey {
readonly FieldReference fieldRef; readonly FieldReference fieldRef;
public FieldReference FieldReference { public FieldReference FieldReference {
@ -281,7 +547,65 @@ namespace de4dot.blocks {
} }
} }
public class MethodReferenceAndDeclaringTypeKey { public class PropertyReferenceAndDeclaringTypeKey : IPropertyReferenceKey {
readonly PropertyReference propRef;
public PropertyReference PropertyReference {
get { return propRef; }
}
public PropertyReferenceAndDeclaringTypeKey(PropertyReference propRef) {
this.propRef = propRef;
}
public override int GetHashCode() {
return MemberReferenceHelper.propertyReferenceHashCode(propRef) +
MemberReferenceHelper.typeHashCode(propRef.DeclaringType);
}
public override bool Equals(object obj) {
var other = obj as PropertyReferenceAndDeclaringTypeKey;
if (other == null)
return false;
return MemberReferenceHelper.comparePropertyReference(propRef, other.propRef) &&
MemberReferenceHelper.compareTypes(propRef.DeclaringType, other.propRef.DeclaringType);
}
public override string ToString() {
return propRef.ToString();
}
}
public class EventReferenceAndDeclaringTypeKey : IEventReferenceKey {
readonly EventReference eventRef;
public EventReference EventReference {
get { return eventRef; }
}
public EventReferenceAndDeclaringTypeKey(EventReference eventRef) {
this.eventRef = eventRef;
}
public override int GetHashCode() {
return MemberReferenceHelper.eventReferenceHashCode(eventRef) +
MemberReferenceHelper.typeHashCode(eventRef.DeclaringType);
}
public override bool Equals(object obj) {
var other = obj as EventReferenceAndDeclaringTypeKey;
if (other == null)
return false;
return MemberReferenceHelper.compareEventReference(eventRef, other.eventRef) &&
MemberReferenceHelper.compareTypes(eventRef.DeclaringType, other.eventRef.DeclaringType);
}
public override string ToString() {
return eventRef.ToString();
}
}
public class MethodReferenceAndDeclaringTypeKey : IMethodReferenceKey {
readonly MethodReference methodRef; readonly MethodReference methodRef;
public MethodReference MethodReference { public MethodReference MethodReference {

View File

@ -32,15 +32,14 @@ namespace de4dot.code.renamer.asmmodules {
} }
class TypeDefDict : RefDict<TypeDef, TypeReference> { class TypeDefDict : RefDict<TypeDef, TypeReference> {
Dictionary<ScopeAndTokenKey, TypeDef> tokenToTypeDef = new Dictionary<ScopeAndTokenKey, TypeDef>(); TypeDefinitionDict<TypeDef> typeToDef = new TypeDefinitionDict<TypeDef>();
Dictionary<TypeReferenceKey, TypeDef> typeRefToDef = new Dictionary<TypeReferenceKey, TypeDef>();
public int Count { public int Count {
get { return tokenToTypeDef.Count; } get { return typeToDef.Count; }
} }
public IEnumerable<TypeDef> getAll() { public IEnumerable<TypeDef> getAll() {
return tokenToTypeDef.Values; return typeToDef.getAll();
} }
public IEnumerable<TypeDef> getSorted() { public IEnumerable<TypeDef> getSorted() {
@ -50,37 +49,27 @@ namespace de4dot.code.renamer.asmmodules {
} }
public TypeDef find(TypeReference typeReference) { public TypeDef find(TypeReference typeReference) {
TypeDef typeDef; return typeToDef.find(typeReference);
if (tokenToTypeDef.TryGetValue(new ScopeAndTokenKey(typeReference), out typeDef))
return typeDef;
typeRefToDef.TryGetValue(new TypeReferenceKey(typeReference), out typeDef);
return typeDef;
} }
public void add(TypeDef typeDef) { public void add(TypeDef typeDef) {
tokenToTypeDef[new ScopeAndTokenKey(typeDef.TypeDefinition)] = typeDef; typeToDef.add(typeDef.TypeDefinition, typeDef);
typeRefToDef[new TypeReferenceKey(typeDef.TypeDefinition)] = typeDef;
} }
public void onTypesRenamed() { public void onTypesRenamed() {
var all = new List<TypeDef>(typeRefToDef.Values); typeToDef.onTypesRenamed();
typeRefToDef.Clear();
foreach (var typeDef in all)
typeRefToDef[new TypeReferenceKey(typeDef.TypeDefinition)] = typeDef;
} }
} }
class FieldDefDict : RefDict<FieldDef, FieldReference> { class FieldDefDict : RefDict<FieldDef, FieldReference> {
Dictionary<ScopeAndTokenKey, FieldDef> tokenToFieldDef = new Dictionary<ScopeAndTokenKey, FieldDef>(); FieldDefinitionDict<FieldDef> fieldToDef = new FieldDefinitionDict<FieldDef>();
Dictionary<FieldReferenceKey, FieldDef> fieldRefToDef = new Dictionary<FieldReferenceKey, FieldDef>();
public int Count { public int Count {
get { return tokenToFieldDef.Count; } get { return fieldToDef.Count; }
} }
public IEnumerable<FieldDef> getAll() { public IEnumerable<FieldDef> getAll() {
return tokenToFieldDef.Values; return fieldToDef.getAll();
} }
public IEnumerable<FieldDef> getSorted() { public IEnumerable<FieldDef> getSorted() {
@ -90,37 +79,27 @@ namespace de4dot.code.renamer.asmmodules {
} }
public FieldDef find(FieldReference fieldReference) { public FieldDef find(FieldReference fieldReference) {
FieldDef fieldDef; return fieldToDef.find(fieldReference);
if (tokenToFieldDef.TryGetValue(new ScopeAndTokenKey(fieldReference), out fieldDef))
return fieldDef;
fieldRefToDef.TryGetValue(new FieldReferenceKey(fieldReference), out fieldDef);
return fieldDef;
} }
public void add(FieldDef fieldDef) { public void add(FieldDef fieldDef) {
tokenToFieldDef[new ScopeAndTokenKey(fieldDef.FieldDefinition)] = fieldDef; fieldToDef.add(fieldDef.FieldDefinition, fieldDef);
fieldRefToDef[new FieldReferenceKey(fieldDef.FieldDefinition)] = fieldDef;
} }
public void onTypesRenamed() { public void onTypesRenamed() {
var all = new List<FieldDef>(fieldRefToDef.Values); fieldToDef.onTypesRenamed();
fieldRefToDef.Clear();
foreach (var fieldDef in all)
fieldRefToDef[new FieldReferenceKey(fieldDef.FieldDefinition)] = fieldDef;
} }
} }
class MethodDefDict : RefDict<MethodDef, MethodReference> { class MethodDefDict : RefDict<MethodDef, MethodReference> {
Dictionary<ScopeAndTokenKey, MethodDef> tokenToMethodDef = new Dictionary<ScopeAndTokenKey, MethodDef>(); MethodDefinitionDict<MethodDef> methodToDef = new MethodDefinitionDict<MethodDef>();
Dictionary<MethodReferenceKey, MethodDef> methodRefToDef = new Dictionary<MethodReferenceKey, MethodDef>();
public int Count { public int Count {
get { return tokenToMethodDef.Count; } get { return methodToDef.Count; }
} }
public IEnumerable<MethodDef> getAll() { public IEnumerable<MethodDef> getAll() {
return tokenToMethodDef.Values; return methodToDef.getAll();
} }
public IEnumerable<MethodDef> getSorted() { public IEnumerable<MethodDef> getSorted() {
@ -130,37 +109,27 @@ namespace de4dot.code.renamer.asmmodules {
} }
public MethodDef find(MethodReference methodReference) { public MethodDef find(MethodReference methodReference) {
MethodDef methodDef; return methodToDef.find(methodReference);
if (tokenToMethodDef.TryGetValue(new ScopeAndTokenKey(methodReference), out methodDef))
return methodDef;
methodRefToDef.TryGetValue(new MethodReferenceKey(methodReference), out methodDef);
return methodDef;
} }
public void add(MethodDef methodDef) { public void add(MethodDef methodDef) {
tokenToMethodDef[new ScopeAndTokenKey(methodDef.MethodDefinition)] = methodDef; methodToDef.add(methodDef.MethodDefinition, methodDef);
methodRefToDef[new MethodReferenceKey(methodDef.MethodDefinition)] = methodDef;
} }
public void onTypesRenamed() { public void onTypesRenamed() {
var all = new List<MethodDef>(methodRefToDef.Values); methodToDef.onTypesRenamed();
methodRefToDef.Clear();
foreach (var methodDef in all)
methodRefToDef[new MethodReferenceKey(methodDef.MethodDefinition)] = methodDef;
} }
} }
class PropertyDefDict : RefDict<PropertyDef, PropertyReference> { class PropertyDefDict : RefDict<PropertyDef, PropertyReference> {
Dictionary<ScopeAndTokenKey, PropertyDef> tokenToPropDef = new Dictionary<ScopeAndTokenKey, PropertyDef>(); PropertyDefinitionDict<PropertyDef> propToDef = new PropertyDefinitionDict<PropertyDef>();
Dictionary<PropertyReferenceKey, PropertyDef> propRefToDef = new Dictionary<PropertyReferenceKey, PropertyDef>();
public int Count { public int Count {
get { return tokenToPropDef.Count; } get { return propToDef.Count; }
} }
public IEnumerable<PropertyDef> getAll() { public IEnumerable<PropertyDef> getAll() {
return tokenToPropDef.Values; return propToDef.getAll();
} }
public IEnumerable<PropertyDef> getSorted() { public IEnumerable<PropertyDef> getSorted() {
@ -170,37 +139,27 @@ namespace de4dot.code.renamer.asmmodules {
} }
public PropertyDef find(PropertyReference propertyReference) { public PropertyDef find(PropertyReference propertyReference) {
PropertyDef propDef; return propToDef.find(propertyReference);
if (tokenToPropDef.TryGetValue(new ScopeAndTokenKey(propertyReference), out propDef))
return propDef;
propRefToDef.TryGetValue(new PropertyReferenceKey(propertyReference), out propDef);
return propDef;
} }
public void add(PropertyDef propDef) { public void add(PropertyDef propDef) {
tokenToPropDef[new ScopeAndTokenKey(propDef.PropertyDefinition)] = propDef; propToDef.add(propDef.PropertyDefinition, propDef);
propRefToDef[new PropertyReferenceKey(propDef.PropertyDefinition)] = propDef;
} }
public void onTypesRenamed() { public void onTypesRenamed() {
var all = new List<PropertyDef>(propRefToDef.Values); propToDef.onTypesRenamed();
propRefToDef.Clear();
foreach (var propDef in all)
propRefToDef[new PropertyReferenceKey(propDef.PropertyDefinition)] = propDef;
} }
} }
class EventDefDict : RefDict<EventDef, EventReference> { class EventDefDict : RefDict<EventDef, EventReference> {
Dictionary<ScopeAndTokenKey, EventDef> tokenToEventDef = new Dictionary<ScopeAndTokenKey, EventDef>(); EventDefinitionDict<EventDef> eventToDef = new EventDefinitionDict<EventDef>();
Dictionary<EventReferenceKey, EventDef> eventRefToDef = new Dictionary<EventReferenceKey, EventDef>();
public int Count { public int Count {
get { return tokenToEventDef.Count; } get { return eventToDef.Count; }
} }
public IEnumerable<EventDef> getAll() { public IEnumerable<EventDef> getAll() {
return tokenToEventDef.Values; return eventToDef.getAll();
} }
public IEnumerable<EventDef> getSorted() { public IEnumerable<EventDef> getSorted() {
@ -210,24 +169,15 @@ namespace de4dot.code.renamer.asmmodules {
} }
public EventDef find(EventReference eventReference) { public EventDef find(EventReference eventReference) {
EventDef eventDef; return eventToDef.find(eventReference);
if (tokenToEventDef.TryGetValue(new ScopeAndTokenKey(eventReference), out eventDef))
return eventDef;
eventRefToDef.TryGetValue(new EventReferenceKey(eventReference), out eventDef);
return eventDef;
} }
public void add(EventDef eventDef) { public void add(EventDef eventDef) {
tokenToEventDef[new ScopeAndTokenKey(eventDef.EventDefinition)] = eventDef; eventToDef.add(eventDef.EventDefinition, eventDef);
eventRefToDef[new EventReferenceKey(eventDef.EventDefinition)] = eventDef;
} }
public void onTypesRenamed() { public void onTypesRenamed() {
var all = new List<EventDef>(eventRefToDef.Values); eventToDef.onTypesRenamed();
eventRefToDef.Clear();
foreach (var eventDef in all)
eventRefToDef[new EventReferenceKey(eventDef.EventDefinition)] = eventDef;
} }
} }
} }