Skip to content

Commit

Permalink
Added a way to check if the type was not already created, this might …
Browse files Browse the repository at this point in the history
…solve some issues when using this in the designer.

[release]
  • Loading branch information
Lakritzator committed Jun 7, 2016
1 parent 001f39e commit ac73206
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
13 changes: 13 additions & 0 deletions Dapplo.InterfaceImpl/IlGeneration/IlTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ public IlTypeBuilder(bool allowSave = false, string assemblyNameString = Default
_moduleBuilder = _assemblyBuilder.DefineDynamicModule(assemblyName.Name, dllName, false);
}


/// <summary>
/// Checks if this type builder already created the type, or return null if not
/// </summary>
/// <param name="fqTypeName">The name of the type</param>
/// <param name="type">The out variable for the type</param>
/// <returns>bool with true if found</returns>
public bool TryGetType(string fqTypeName, out Type type)
{
type = _assemblyBuilder.GetTypes().Where(x => x.FullName == fqTypeName).FirstOrDefault();
return type != null;
}

/// <summary>
/// Creates an implementation as Type for a given interface, which can be intercepted
/// </summary>
Expand Down
43 changes: 25 additions & 18 deletions Dapplo.InterfaceImpl/InterceptorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static void DefineImplementationTypeForInterface(Type interfaceType, Type
public static TResult New<TResult>()
{
// create the intercepted object
return (TResult) New(typeof (TResult));
return (TResult)New(typeof(TResult));
}

/// <summary>
Expand Down Expand Up @@ -141,31 +141,38 @@ public static IExtensibleInterceptor New(Type interfaceType)
{
if (!TypeMap.TryGetValue(interfaceType, out implementingType))
{
// Use this baseType if nothing is specified
var baseType = typeof(ExtensibleInterceptorImpl<>);
foreach (var implementingInterface in implementingInterfaces)
{
if (BaseTypeMap.ContainsKey(implementingInterface))
{
baseType = BaseTypeMap[implementingInterface];
break;
}
}
// Make sure we have a non generic type, by filling in the "blanks"
if (baseType.IsGenericType)
{
baseType = baseType.MakeGenericType(interfaceType);
}

// Build a name for the type
var typeName = interfaceType.Name + "Impl";
// Remove "I" at the start
if (typeName.StartsWith("I"))
{
typeName = typeName.Substring(1);
}

var fqTypeName = interfaceType.FullName.Replace(interfaceType.Name, typeName);
implementingType = TypeBuilder.CreateType(fqTypeName, implementingInterfaces.ToArray(), baseType);

// Only create it if it was not already created via another way
if (!TypeBuilder.TryGetType(fqTypeName, out implementingType))
{
// Use this baseType if nothing is specified
var baseType = typeof(ExtensibleInterceptorImpl<>);
foreach (var implementingInterface in implementingInterfaces)
{
if (BaseTypeMap.ContainsKey(implementingInterface))
{
baseType = BaseTypeMap[implementingInterface];
break;
}
}
// Make sure we have a non generic type, by filling in the "blanks"
if (baseType.IsGenericType)
{
baseType = baseType.MakeGenericType(interfaceType);
}


implementingType = TypeBuilder.CreateType(fqTypeName, implementingInterfaces.ToArray(), baseType);
}

// Register the implementation for the interface
TypeMap.AddOrOverwrite(interfaceType, implementingType);
Expand Down

0 comments on commit ac73206

Please sign in to comment.