-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
266 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using ExpressionDebugger; | ||
using System; | ||
using System.IO; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
|
||
namespace Mapster.Diagnostics | ||
{ | ||
public class DebugInfoInjectorEx: DebugInfoInjector | ||
{ | ||
public DebugInfoInjectorEx(string filename): base (filename) { } | ||
|
||
public DebugInfoInjectorEx(TextWriter writer): base(writer) { } | ||
|
||
protected override Expression VisitConstant(ConstantExpression node) | ||
{ | ||
node = (ConstantExpression)base.VisitConstant(node); | ||
|
||
if (CanEmitConstant(node.Value, node.Type)) | ||
return node; | ||
|
||
return GetNonPublicObject(node.Value, node.Type); | ||
} | ||
|
||
private static Expression GetNonPublicObject(object value, Type type) | ||
{ | ||
var i = GlobalReference.GetIndex(value); | ||
return Expression.Convert( | ||
Expression.Call( | ||
typeof(GlobalReference).GetMethod(nameof(GlobalReference.GetObject)), | ||
Expression.Constant(i)), | ||
type); | ||
} | ||
|
||
private static bool CanEmitConstant(object value, Type type) | ||
{ | ||
if (value == null | ||
|| type.IsPrimitive | ||
|| type == typeof(string) | ||
|| type == typeof(decimal)) | ||
return true; | ||
|
||
if (value is Type t) | ||
return IsVisible(t); | ||
|
||
if (value is MethodBase mb) | ||
return IsVisible(mb); | ||
|
||
return false; | ||
} | ||
|
||
private static bool IsVisible(Type t) | ||
{ | ||
return t is TypeBuilder | ||
|| t.IsGenericParameter | ||
|| t.IsVisible; | ||
} | ||
|
||
private static bool IsVisible(MethodBase mb) | ||
{ | ||
if (mb is DynamicMethod || !mb.IsPublic) | ||
return false; | ||
|
||
Type dt = mb.DeclaringType; | ||
return dt == null || IsVisible(dt); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace ExpressionDebugger | ||
{ | ||
public static class GlobalReference | ||
{ | ||
private static readonly List<object> _references = new List<object>(); | ||
private static readonly Dictionary<object, int> _dict = new Dictionary<object, int>(); | ||
|
||
public static int GetIndex(object obj) | ||
{ | ||
if (_dict.TryGetValue(obj, out var id)) | ||
return id; | ||
lock (_references) | ||
{ | ||
if (_dict.TryGetValue(obj, out id)) | ||
return id; | ||
id = _references.Count; | ||
_references.Add(obj); | ||
_dict[obj] = id; | ||
return id; | ||
} | ||
} | ||
|
||
public static object GetObject(int i) | ||
{ | ||
return _references[i]; | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/Mapster.Tests/Diagnostics/TypeAdapterConfigExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Mapster.Diagnostics; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Reflection; | ||
|
||
namespace Mapster | ||
{ | ||
public static class TypeAdapterConfigExtensions | ||
{ | ||
static Dictionary<string, int> _registeredFilename; | ||
|
||
[Conditional("DEBUG")] | ||
public static void EnableDebugging(this TypeAdapterConfig config, string sourceCodePath = null) | ||
{ | ||
if (sourceCodePath == null) | ||
sourceCodePath = GetDefaultSourceCodePath(); | ||
|
||
//initialize on first call | ||
if (_registeredFilename == null) | ||
{ | ||
_registeredFilename = new Dictionary<string, int>(); | ||
_assemblyName = GetAssemblyName(); | ||
} | ||
|
||
config.Compiler = lambda => | ||
{ | ||
var filename = lambda.Parameters[0].Type.Name + "-" + lambda.ReturnType.Name; | ||
var key = filename; | ||
lock (_registeredFilename) | ||
{ | ||
if (!_registeredFilename.TryGetValue(key, out var num)) | ||
_registeredFilename[key] = 0; | ||
else | ||
filename += "-" + num; | ||
_registeredFilename[key]++; | ||
} | ||
using (var injector = new DebugInfoInjectorEx(Path.Combine(sourceCodePath, filename + ".cs"))) | ||
{ | ||
return injector.Compile(lambda, _assemblyName); | ||
} | ||
}; | ||
} | ||
|
||
private static string GetDefaultSourceCodePath() | ||
{ | ||
var dir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Mapster"); | ||
if (!Directory.Exists(dir)) | ||
Directory.CreateDirectory(dir); | ||
return dir; | ||
} | ||
|
||
static AssemblyName _assemblyName; | ||
private static AssemblyName GetAssemblyName() | ||
{ | ||
StrongNameKeyPair kp; | ||
// Getting this from a resource would be a good idea. | ||
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Mapster.Tests.mock.keys")) | ||
using (var mem = new MemoryStream()) | ||
{ | ||
stream.CopyTo(mem); | ||
mem.Position = 0; | ||
kp = new StrongNameKeyPair(mem.ToArray()); | ||
} | ||
var name = "Mapster.Dynamic"; | ||
return new AssemblyName(name) { KeyPair = kp }; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.