From afbcb925e4052421ac7c4fedc4b194ebb0e77e56 Mon Sep 17 00:00:00 2001 From: Manuel Carrasco Date: Fri, 25 Oct 2019 16:16:14 -0300 Subject: [PATCH 1/4] Added model for property definitions. --- Model/Types/TypeDefinitions.cs | 61 +++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/Model/Types/TypeDefinitions.cs b/Model/Types/TypeDefinitions.cs index 27062ea7..637db38d 100644 --- a/Model/Types/TypeDefinitions.cs +++ b/Model/Types/TypeDefinitions.cs @@ -397,7 +397,65 @@ public override bool Equals(object obj) return result; } } + public class PropertyDefinition : ITypeMemberDefinition + { + public PropertyDefinition(string name, IType propType) + { + PropertyType = propType; + Name = name; + Attributes = new HashSet(); + } + + public ISet Attributes { get; private set; } + public IType PropertyType { get; set; } + public string Name { get; set; } + public MethodDefinition Getter { get; set; } + public MethodDefinition Setter { get; set; } + public TypeDefinition ContainingType { get; set; } + IBasicType ITypeMemberReference.ContainingType + { + get { return this.ContainingType; } + } + public bool MatchReference(ITypeMemberReference member) + { + if (member is PropertyDefinition) + return member.Equals(this); + return false; + } + public override bool Equals(object obj) + { + if (obj is PropertyDefinition propertyDef) + { + bool hasSetter = (propertyDef.Setter != null) == (this.Setter != null); + bool hasGetter = (propertyDef.Getter != null) == (this.Getter != null); + return propertyDef.Name.Equals(this.Name) && + propertyDef.PropertyType.Equals(this.PropertyType) && + hasSetter && hasGetter && + (propertyDef.Getter == null || propertyDef.Getter.Equals(this.Getter)) && + (propertyDef.Setter == null || propertyDef.Setter.Equals(this.Setter)) && + (propertyDef.ContainingType.Equals(this.ContainingType)); + } + return false; + } + public override string ToString() + { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.AppendLine("Property definition"); + stringBuilder.AppendLine(String.Format("Name: {0}", Name)); + stringBuilder.AppendLine(String.Format("Property type: {0}", PropertyType)); + stringBuilder.AppendLine(String.Format("Containing type: {0}", ContainingType)); + if (Getter != null) + stringBuilder.AppendLine(String.Format("Getter: {0}", Getter.ToSignatureString())); + if (Setter != null) + stringBuilder.AppendLine(String.Format("Setter: {0}", Setter.ToSignatureString())); + return stringBuilder.ToString(); + } + public override int GetHashCode() + { + return this.Name.GetHashCode(); + } + } public class MethodDefinition : ITypeMemberDefinition, IMethodReference, IGenericDefinition { public VisibilityKind Visibility { get; set; } @@ -623,7 +681,7 @@ public class TypeDefinition : IBasicType, IGenericDefinition, ITypeMemberDefinit public IList Methods { get; private set; } public IList Types { get; private set; } public IBasicType UnderlayingType { get; set; } - + public ISet PropertyDefinitions { get; private set; } public TypeDefinition(string name, TypeKind typeKind = TypeKind.Unknown, TypeDefinitionKind kind = TypeDefinitionKind.Unknown) { this.Name = name; @@ -635,6 +693,7 @@ public TypeDefinition(string name, TypeKind typeKind = TypeKind.Unknown, TypeDef this.Fields = new List(); this.Methods = new List(); this.Types = new List(); + this.PropertyDefinitions = new HashSet(); } public string GenericName From a39243bf49640c4d343cd18f77c687c9699721ab Mon Sep 17 00:00:00 2001 From: fcurdi Date: Tue, 25 Feb 2020 16:37:27 -0300 Subject: [PATCH 2/4] extract properties --- MetadataProvider/AssemblyExtractor.cs | 20 ++++++++++++++++++++ Model/Types/TypeDefinitions.cs | 1 + 2 files changed, 21 insertions(+) diff --git a/MetadataProvider/AssemblyExtractor.cs b/MetadataProvider/AssemblyExtractor.cs index 66787d90..934ca112 100644 --- a/MetadataProvider/AssemblyExtractor.cs +++ b/MetadataProvider/AssemblyExtractor.cs @@ -281,6 +281,11 @@ private void ExtractType(SRM.TypeDefinitionHandle typedefHandle) } defGenericContext.TypeParameters.Clear(); + + foreach (var handle in typedef.GetProperties()) + { + ExtractProperty(handle); + } foreach (var handle in typedef.GetNestedTypes()) { @@ -495,6 +500,21 @@ private Constant ExtractFieldDefaultValue(SRM.FieldDefinition fielddef) return result; } + + private void ExtractProperty(SRM.PropertyDefinitionHandle handle) + { + var propertyDef = metadata.GetPropertyDefinition(handle); + var name = metadata.GetString(propertyDef.Name); + var signature = propertyDef.DecodeSignature(signatureTypeProvider, defGenericContext); + var property = new PropertyDefinition(name, signature.ReturnType) + { + Getter = !propertyDef.GetAccessors().Getter.IsNil ? GetDefinedMethod(propertyDef.GetAccessors().Getter) : default, + Setter = !propertyDef.GetAccessors().Setter.IsNil ? GetDefinedMethod(propertyDef.GetAccessors().Setter) : default, + ContainingType = currentType, + IsInstanceProperty = signature.Header.IsInstance + }; + currentType.PropertyDefinitions.Add(property); + } private void ExtractMethod(SRM.MethodDefinitionHandle methoddefHandle) { diff --git a/Model/Types/TypeDefinitions.cs b/Model/Types/TypeDefinitions.cs index 637db38d..343e4932 100644 --- a/Model/Types/TypeDefinitions.cs +++ b/Model/Types/TypeDefinitions.cs @@ -423,6 +423,7 @@ public bool MatchReference(ITypeMemberReference member) return false; } + public bool IsInstanceProperty { get; set; } public override bool Equals(object obj) { if (obj is PropertyDefinition propertyDef) From 406da635c331484b7b8c2f3e6d5d34ccea9836bf Mon Sep 17 00:00:00 2001 From: fcurdi Date: Tue, 25 Feb 2020 16:42:50 -0300 Subject: [PATCH 3/4] refactor --- MetadataProvider/AssemblyExtractor.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/MetadataProvider/AssemblyExtractor.cs b/MetadataProvider/AssemblyExtractor.cs index 934ca112..483338b0 100644 --- a/MetadataProvider/AssemblyExtractor.cs +++ b/MetadataProvider/AssemblyExtractor.cs @@ -506,10 +506,12 @@ private void ExtractProperty(SRM.PropertyDefinitionHandle handle) var propertyDef = metadata.GetPropertyDefinition(handle); var name = metadata.GetString(propertyDef.Name); var signature = propertyDef.DecodeSignature(signatureTypeProvider, defGenericContext); + var getter = propertyDef.GetAccessors().Getter; + var setter = propertyDef.GetAccessors().Setter; var property = new PropertyDefinition(name, signature.ReturnType) { - Getter = !propertyDef.GetAccessors().Getter.IsNil ? GetDefinedMethod(propertyDef.GetAccessors().Getter) : default, - Setter = !propertyDef.GetAccessors().Setter.IsNil ? GetDefinedMethod(propertyDef.GetAccessors().Setter) : default, + Getter = !getter.IsNil ? GetDefinedMethod(getter) : default, + Setter = !setter.IsNil ? GetDefinedMethod(setter) : default, ContainingType = currentType, IsInstanceProperty = signature.Header.IsInstance }; From 87be0af238fdb64f881a4445289ce07da12f6488 Mon Sep 17 00:00:00 2001 From: fcurdi Date: Tue, 25 Feb 2020 16:45:57 -0300 Subject: [PATCH 4/4] styling --- Model/Types/TypeDefinitions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model/Types/TypeDefinitions.cs b/Model/Types/TypeDefinitions.cs index 343e4932..4d706aae 100644 --- a/Model/Types/TypeDefinitions.cs +++ b/Model/Types/TypeDefinitions.cs @@ -416,6 +416,7 @@ IBasicType ITypeMemberReference.ContainingType { get { return this.ContainingType; } } + public bool IsInstanceProperty { get; set; } public bool MatchReference(ITypeMemberReference member) { if (member is PropertyDefinition) @@ -423,7 +424,6 @@ public bool MatchReference(ITypeMemberReference member) return false; } - public bool IsInstanceProperty { get; set; } public override bool Equals(object obj) { if (obj is PropertyDefinition propertyDef)