Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c9ff874

Browse files
committed
fixed problem with processing types with parent in another assembly
1 parent 6a4ead5 commit c9ff874

5 files changed

Lines changed: 113 additions & 56 deletions

File tree

AssemblyToProcess/AssemblyToProcess.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@
4141
</ItemGroup>
4242
<ItemGroup>
4343
<Compile Include="AssemblyInfo.cs" />
44-
<Compile Include="Child.cs" />
44+
<Compile Include="Child.cs">
45+
<SubType>Code</SubType>
46+
</Compile>
4547
<Compile Include="ClassWithIgnoredProperties.cs">
4648
<SubType>Code</SubType>
4749
</Compile>
48-
<Compile Include="NullableClass.cs" />
4950
<Compile Include="EnumClass.cs">
5051
<SubType>Code</SubType>
5152
</Compile>
@@ -64,6 +65,9 @@
6465
<Compile Include="NormalStruct.cs">
6566
<SubType>Code</SubType>
6667
</Compile>
68+
<Compile Include="NullableClass.cs">
69+
<SubType>Code</SubType>
70+
</Compile>
6771
<Compile Include="ObjectCollection.cs">
6872
<SubType>Code</SubType>
6973
</Compile>

CommonAssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System.Reflection;
22
[assembly: AssemblyTitle("Foody.ToString")]
33
[assembly: AssemblyProduct("Foody.ToString")]
4-
[assembly: AssemblyVersion("1.5.4.0")]
5-
[assembly: AssemblyFileVersion("1.5.4.0")]
4+
[assembly: AssemblyVersion("1.5.5.0")]
5+
[assembly: AssemblyFileVersion("1.5.5.0")]

Fody/Extensions/PropertyDefinitionExtensions.cs

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,24 @@ namespace ToString.Fody.Extensions
55
{
66
public static class PropertyDefinitionExtensions
77
{
8-
public static MethodReference GetGetMethod(this PropertyDefinition property, TypeDefinition targetType)
8+
public static MethodReference GetGetMethod(this PropertyDefinition property, TypeReference targetType)
99
{
10-
MethodReference get;
11-
12-
if (property.DeclaringType.HasGenericParameters)
10+
MethodReference method = property.GetMethod;
11+
if (method.DeclaringType.HasGenericParameters)
1312
{
14-
var type = property.DeclaringType;
15-
16-
GenericInstanceType genericInstanceType;
17-
18-
TypeDefinition parent = targetType;
19-
TypeReference parentReference = targetType;
20-
21-
if (property.DeclaringType == targetType)
22-
{
23-
genericInstanceType = GetGenericInstanceType(type, type.GenericParameters);
24-
}
25-
else
26-
{
27-
var propertyType = property.DeclaringType.Resolve();
28-
29-
while (propertyType.FullName != parent.Resolve().FullName)
30-
{
31-
parentReference = parent.BaseType;
32-
parent = parent.BaseType.Resolve();
33-
}
34-
35-
genericInstanceType = parentReference as GenericInstanceType;
36-
if (genericInstanceType == null)
37-
{
38-
genericInstanceType = GetGenericInstanceType(type, parentReference.GenericParameters);
39-
}
40-
}
41-
42-
TypeReference returnType = property.PropertyType;
43-
44-
45-
get = new MethodReference(property.GetMethod.Name, returnType)
13+
var genericInstanceType = property.DeclaringType.GetGenericInstanceType(targetType);
14+
MethodReference newRef = new MethodReference(method.Name, method.ReturnType)
4615
{
47-
DeclaringType = property.Module.Import(genericInstanceType.Resolve()),
16+
DeclaringType = genericInstanceType,
4817
HasThis = true
4918
};
19+
20+
return newRef;
5021
}
5122
else
5223
{
53-
get = property.GetMethod;
54-
}
55-
56-
return get;
57-
}
58-
59-
private static GenericInstanceType GetGenericInstanceType(TypeDefinition type, Collection<GenericParameter> parameters)
60-
{
61-
var genericInstanceType = new GenericInstanceType(type);
62-
foreach (var genericParameter in parameters)
63-
{
64-
genericInstanceType.GenericArguments.Add(genericParameter);
24+
return method;
6525
}
66-
return genericInstanceType;
6726
}
6827
}
6928
}

Fody/Extensions/TypeDefinitionExtensions.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33

44
using Mono.Cecil;
5+
using Mono.Collections.Generic;
56

67
public static class TypeDefinitionExtensions
78
{
@@ -29,4 +30,83 @@ public static PropertyDefinition[] GetProperties(this TypeDefinition type)
2930

3031
return properties.ToArray();
3132
}
33+
34+
public static TypeReference GetGenericInstanceType(this TypeReference type, TypeReference targetType)
35+
{
36+
var genericInstance = targetType as GenericInstanceType;
37+
if (genericInstance != null)
38+
{
39+
return genericInstance;
40+
}
41+
42+
if (type.IsGenericParameter)
43+
{
44+
var genericParameter = type as GenericParameter;
45+
46+
var current = targetType;
47+
var currentResolved = current.Resolve();
48+
49+
while (currentResolved.FullName != genericParameter.DeclaringType.FullName)
50+
{
51+
if (currentResolved.BaseType == null)
52+
{
53+
return type;
54+
}
55+
current = currentResolved.BaseType;
56+
currentResolved = current.Resolve();
57+
}
58+
59+
var genericInstanceType = current as GenericInstanceType;
60+
if (genericInstanceType != null)
61+
{
62+
var newType = genericInstanceType.GenericArguments[genericParameter.Position];
63+
return newType;
64+
}
65+
66+
return type;
67+
}
68+
69+
if (type.HasGenericParameters)
70+
{
71+
GenericInstanceType genericInstanceType;
72+
TypeReference parent = targetType;
73+
TypeReference parentReference = targetType;
74+
75+
if (type.FullName == targetType.Resolve().FullName)
76+
{
77+
genericInstanceType = GetGenericInstanceType(type, type.GenericParameters);
78+
}
79+
else
80+
{
81+
var propertyType = type.Resolve();
82+
83+
TypeDefinition parentResolved;
84+
while (parent != null && propertyType.FullName != (parentResolved = parent.Resolve()).FullName)
85+
{
86+
parentReference = parentResolved.BaseType;
87+
parent = parentResolved.BaseType != null ? parentResolved.BaseType.Resolve() : null;
88+
}
89+
90+
genericInstanceType = parentReference as GenericInstanceType;
91+
if (genericInstanceType == null)
92+
{
93+
genericInstanceType = GetGenericInstanceType(type, parentReference.GenericParameters);
94+
}
95+
}
96+
97+
return genericInstanceType;
98+
}
99+
100+
return type;
101+
}
102+
103+
private static GenericInstanceType GetGenericInstanceType(TypeReference type, Collection<GenericParameter> parameters)
104+
{
105+
var genericInstanceType = new GenericInstanceType(type);
106+
foreach (var genericParameter in parameters)
107+
{
108+
genericInstanceType.GenericArguments.Add(genericParameter);
109+
}
110+
return genericInstanceType;
111+
}
32112
}

Fody/ModuleWeaver.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ private void AddToString(TypeDefinition type)
7171
var format = GetFormatString(type, properties);
7272

7373
var body = method.Body;
74+
body.InitLocals = true;
7475
var ins = body.Instructions;
7576

7677
var hasCollections = properties.Any(x => !x.PropertyType.IsGenericParameter && x.PropertyType.Resolve().IsCollection());
@@ -283,9 +284,13 @@ private void AddPropertyCode(MethodBody body, int index, PropertyDefinition prop
283284
this.If(ins,
284285
c =>
285286
{
286-
ins.Add(Instruction.Create(OpCodes.Dup));
287+
ins.Add(Instruction.Create(OpCodes.Dup));
288+
AddBoxing(property, targetType, c);
289+
},
290+
t =>
291+
{
292+
AddBoxing(property, targetType, t);
287293
},
288-
t => {},
289294
e =>
290295
{
291296
ins.Add(Instruction.Create(OpCodes.Pop));
@@ -297,6 +302,15 @@ private void AddPropertyCode(MethodBody body, int index, PropertyDefinition prop
297302
ins.Add(Instruction.Create(OpCodes.Stelem_Ref));
298303
}
299304

305+
private static void AddBoxing(PropertyDefinition property, TypeDefinition targetType, Collection<Instruction> ins)
306+
{
307+
if (property.PropertyType.IsValueType || property.PropertyType.IsGenericParameter)
308+
{
309+
var genericType = property.PropertyType.GetGenericInstanceType(targetType);
310+
ins.Add(Instruction.Create(OpCodes.Box, genericType));
311+
}
312+
}
313+
300314
private void NewStringBuilder(Collection<Instruction> ins)
301315
{
302316
var stringBuilderConstructor = this.ModuleDefinition.Import(typeof (StringBuilder).GetConstructor(new Type[] {}));

0 commit comments

Comments
 (0)