Skip to content

Commit

Permalink
Remove name2GenericType from MethodEmitter
Browse files Browse the repository at this point in the history
Because `name2GenericType` is in practice always an empty dictionary,
the helper methods `ExtractCorrectType` and `ExtractParametersTypes` in
`GenericUtil` don't really do anything. They can be removed since noone
else is using them.

(Side note: it looks like `ExtractCorrectType` might have malfunctioned,
had it ever actually done anything: it maps parameters `T` -> `Tmapped`
and arrays `T[]` -> `Tmapped[]`, but forgets about instantiatd generic
types involving `T` as an argument, such as `IEnumerable<T>`.)
  • Loading branch information
stakx committed Mar 25, 2021
1 parent cf4ad36 commit c90b6af
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 68 deletions.
66 changes: 3 additions & 63 deletions src/Castle.Core/DynamicProxy/Generators/Emitters/GenericUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,70 +37,10 @@ public static GenericTypeParameterBuilder[] CopyGenericArguments(

public static GenericTypeParameterBuilder[] CopyGenericArguments(
MethodInfo methodToCopyGenericsFrom,
MethodBuilder builder,
Dictionary<string, GenericTypeParameterBuilder> name2GenericType)
MethodBuilder builder)
{
return
CopyGenericArguments(methodToCopyGenericsFrom, name2GenericType,
builder.DefineGenericParameters);
}

public static Type ExtractCorrectType(Type paramType, Dictionary<string, GenericTypeParameterBuilder> name2GenericType)
{
if (paramType.IsArray)
{
var rank = paramType.GetArrayRank();

var underlyingType = paramType.GetElementType();

if (underlyingType.IsGenericParameter)
{
GenericTypeParameterBuilder genericType;
if (name2GenericType.TryGetValue(underlyingType.Name, out genericType) == false)
{
return paramType;
}

if (rank == 1)
{
return genericType.MakeArrayType();
}
return genericType.MakeArrayType(rank);
}
if (rank == 1)
{
return underlyingType.MakeArrayType();
}
return underlyingType.MakeArrayType(rank);
}

if (paramType.IsGenericParameter)
{
GenericTypeParameterBuilder value;
if (name2GenericType.TryGetValue(paramType.Name, out value))
{
return value;
}
}

return paramType;
}

public static Type[] ExtractParametersTypes(
ParameterInfo[] baseMethodParameters,
Dictionary<string, GenericTypeParameterBuilder> name2GenericType)
{
var newParameters = new Type[baseMethodParameters.Length];

for (var i = 0; i < baseMethodParameters.Length; i++)
{
var param = baseMethodParameters[i];
var paramType = param.ParameterType;

newParameters[i] = ExtractCorrectType(paramType, name2GenericType);
}

return newParameters;
var _ = new Dictionary<string, GenericTypeParameterBuilder>();
return CopyGenericArguments(methodToCopyGenericsFrom, _, builder.DefineGenericParameters);
}

private static Type AdjustConstraintToNewGenericParameters(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,11 @@ internal MethodEmitter(AbstractTypeEmitter owner, string name,
// proxy type generation code. At present, proxy types are never generic.
Debug.Assert(owner.GenericTypeParams == null || owner.GenericTypeParams.Length == 0);

var name2GenericType = new Dictionary<string, GenericTypeParameterBuilder>(0);

var returnType = GenericUtil.ExtractCorrectType(methodToUseAsATemplate.ReturnType, name2GenericType);
var returnType = methodToUseAsATemplate.ReturnType;
var baseMethodParameters = methodToUseAsATemplate.GetParameters();
var parameters = GenericUtil.ExtractParametersTypes(baseMethodParameters, name2GenericType);
var parameters = ArgumentsUtil.GetTypes(baseMethodParameters);

genericTypeParams = GenericUtil.CopyGenericArguments(methodToUseAsATemplate, builder, name2GenericType);
genericTypeParams = GenericUtil.CopyGenericArguments(methodToUseAsATemplate, builder);
SetParameters(parameters);
SetReturnType(returnType);
SetSignature(returnType, methodToUseAsATemplate.ReturnParameter, parameters, baseMethodParameters);
Expand Down

0 comments on commit c90b6af

Please sign in to comment.