ObjectCloner icon indicating copy to clipboard operation
ObjectCloner copied to clipboard

Exception: No coercion operator is defined between types 'System.Void*' and 'System.Object'.

Open heischo opened this issue 5 years ago • 2 comments

Hi.

I get this exception when I try to clone the object 'NLog.LogManager.Configuration' of type NLog.Config.LoggingConfiguration. I use the nuget version 2.2.2 in a .NET Core 3.1 project (Console App).

Stack Trace:

   at System.Linq.Expressions.Expression.GetUserDefinedCoercionOrThrow(ExpressionType coercionType, Expression expression, Type convertToType)
   at System.Linq.Expressions.Expression.Convert(Expression expression, Type type, MethodInfo method)
   at System.Linq.Expressions.Expression.Convert(Expression expression, Type type)
   at ObjectCloner.Internal.DeepCloneExpressionBuilder.CreateRecursiveCallExpression(Expression objectToCopy)
   at ObjectCloner.Internal.DeepCloneExpressionBuilder.<CreateFieldCopyExpressions>d__19.MoveNext()
   at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
   at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection)
   at ObjectCloner.Internal.DeepCloneExpressionBuilder.Build()
   at ObjectCloner.Internal.DeepCloneInternal.<>c__DisplayClass1_0.<GetDeepCloner>b__0(Type t)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at ObjectCloner.Internal.DeepCloneInternal.GetDeepCloner(Type type)
   at ObjectCloner.ObjectCloner.DeepClone[T](T original)

heischo avatar Feb 24 '21 07:02 heischo

Hello,

Thanks for reporting the error. Do you happen to have a repo that contains a repro steps? It's wild guessing without one

marcelltoth avatar Feb 26 '21 14:02 marcelltoth

@marcelltoth Hi, no, I don't have a public repo in this case. But I thought it will not be 'wild guessing', because I mentioned clearly which object I try to clone. Simply use the following code, after installing the nuget package for NLog:

var config = new NLog.Config.LoggingConfiguration();

// Targets where to log to: File and Console
var logfile = new NLog.Targets.FileTarget("logfile") { FileName = "file.txt" };
var logconsole = new NLog.Targets.ConsoleTarget("logconsole");
            
// Rules for mapping loggers to targets            
config.AddRule(LogLevel.Info, LogLevel.Fatal, logconsole);
config.AddRule(LogLevel.Debug, LogLevel.Fatal, logfile);
            
// Apply config           
NLog.LogManager.Configuration = config;

(see https://github.com/NLog/NLog/wiki/Tutorial)

Then, you can try to clone NLog.LogManager.Configuration.

heischo avatar Mar 03 '21 09:03 heischo