I am adding methods to a static class to copy all property values of a non static class instance to a static class instance. I am getting the following exceptions:
Exception thrown: 'System.Reflection.TargetException' in mscorlib.dll System.Reflection.TargetException: Object does not match target type. at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target) at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index) at Dispensing.Classes.Options.CopyPropertiesFromSource(Object source) in C:\Users\erics\source\repos\TestingEFClassLibrary\TestingEFClassLibrary\Classes\Options.cs:line 32
My static class looks like this:
public static class Options
{
public static bool DispenseFile { get; set; }
public static Units Units { get; set; }
public static AppSettings AppSettings { get; set; }
public static DispenseStyle DispenseStyle { get; set; }
public static double Volume { get; set; }
public static void CopyPropertiesFromSource(object source)
{
Type t = typeof(Options);
var destinationProperties = t.GetProperties(BindingFlags.Static | BindingFlags.Public);
var sourceProperties = source.GetType().GetProperties();
foreach (var destinationProperty in destinationProperties)
{
foreach (var sourceProperty in sourceProperties)
{
if (destinationProperty.Name == sourceProperty.Name && destinationProperty.PropertyType == sourceProperty.PropertyType)
{
try
{
Debug.WriteLine(destinationProperty.Name);
destinationProperty.SetValue(t, sourceProperty.GetValue(sourceProperty));
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
break;
}
}
}
}
}
I am getting the exceptions on the line:
destinationProperty.SetValue(t, sourceProperty);
When this happens the destinationProperty is Boolean and the sourceProperty is also Boolean.
Aucun commentaire:
Enregistrer un commentaire