C# Copy all object parameters to child class object - c#

Suppose I have an object of a parent class which I can't change - for example, an instance of ListBox with long list of parameters. Now I create a child class:
class PlaylistBox : ListBox
{
void CopySettingsFrom(ListBox In)
{
//...what now?
}
}
Question - how can I efficiently make a shallow copy from In object to the new object of PlaylistBox?

Here an example with 3 methods, based on reflection and AutoMapper with explanation:
internal class Program
{
private static void Main(string[] args)
{
Example1();
Example2();
Example3();
}
public static void Example1()
{
Console.WriteLine("This example shows using copy with reflection. Minus of this method - u have to implement FULL copy for each class or u will copy only references to object properties");
//creating new parent class with some values
var parentClass = new ParentClass
{
Property1 = "qqq",
Property2 = 1,
ObjectProperty = new SomeClassWithObjectProperty
{
ObjectProperty = new SomeObjectClass {SomeProperty = "www"}
}
};
//crating new child class and copy REFERENCES to properties
var childClassReflection = new ChildClassReflection(parentClass);
//changing properties of parent
parentClass.Property1 = "rrr";
parentClass.Property2 = 2;
parentClass.ObjectProperty.ObjectProperty.SomeProperty = "eee";
//we will get OLD values for VALUE types and OLD values for REFERENCE types
//qqq 1 WWW
Console.WriteLine(childClassReflection.Property1 + " " + childClassReflection.Property2 + " " + childClassReflection.ObjectProperty.ObjectProperty.SomeProperty);
}
public static void Example2()
{
Console.WriteLine();
Console.WriteLine("This example shows using copy with reflection WITH FULL COPY");
//creating new parent class with some values
var parentClass = new ParentClass
{
Property1 = "qqq",
Property2 = 1,
ObjectProperty = new SomeClassWithObjectProperty
{
ObjectProperty = new SomeObjectClass {SomeProperty = "www"}
}
};
//crating new child class and copy REFERENCES to properties
var childClassReflection = new ChildClassReflectionWithFullCopy(parentClass);
//changing properties of parent
parentClass.Property1 = "rrr";
parentClass.Property2 = 2;
parentClass.ObjectProperty.ObjectProperty.SomeProperty = "eee";
//we will get OLD values for VALUE types and NEW values for REFERENCE types
//qqq 1 eee
Console.WriteLine(childClassReflection.Property1 + " " + childClassReflection.Property2 + " " + childClassReflection.ObjectProperty.ObjectProperty.SomeProperty);
}
public static void Example3()
{
//here i will show copy using AutoMapper
Console.WriteLine();
Console.WriteLine("This example shows using copy with AutoMapper");
//creating new parent class with some values
var parentClass = new ParentClass
{
Property1 = "qqq",
Property2 = 1,
ObjectProperty = new SomeClassWithObjectProperty
{
ObjectProperty = new SomeObjectClass { SomeProperty = "www" }
}
};
Mapper.Initialize(cfg => cfg.CreateMap<ParentClass, ChildClassAutoMapper>());
//crating new child class and copy REFERENCES to properties
var childClassReflection = Mapper.Map<ChildClassAutoMapper>(parentClass);
//changing properties of parent
parentClass.Property1 = "rrr";
parentClass.Property2 = 2;
parentClass.ObjectProperty.ObjectProperty.SomeProperty = "eee";
//we will get OLD values for VALUE types and OLD values for REFERENCE types
//qqq 1 eee
Console.WriteLine(childClassReflection.Property1 + " " + childClassReflection.Property2 + " " + childClassReflection.ObjectProperty.ObjectProperty.SomeProperty);
}
}
public class ChildClassAutoMapper:ParentClass
{
}
public class ChildClassReflection : ParentClass
{
public ChildClassReflection(ParentClass parentClass)
{
foreach (var p in ParentProperties)
p.SetMethod.Invoke(this, new[] {p.GetMethod.Invoke(parentClass, null)});
}
//do it only once for best performance
private static PropertyInfo[] ParentProperties { get; } = typeof(ParentClass).GetProperties().Where(c => c.CanRead && c.CanWrite).ToArray();
}
public class ChildClassReflectionWithFullCopy : ParentClass
{
public ChildClassReflectionWithFullCopy(ParentClass parentClass)
{
var parentClassLocal = JsonConvert.DeserializeObject<ParentClass>(JsonConvert.SerializeObject(parentClass));
foreach (var p in ParentProperties)
p.SetMethod.Invoke(this, new[] {p.GetMethod.Invoke(parentClassLocal, null)});
}
//do it only once for best performance
private static PropertyInfo[] ParentProperties { get; } = typeof(ParentClass).GetProperties().Where(c => c.CanRead && c.CanWrite).ToArray();
}
public class ParentClass
{
public string Property1 { get; set; }
public int Property2 { get; set; }
public SomeClassWithObjectProperty ObjectProperty { get; set; }
}
public class SomeClassWithObjectProperty
{
public SomeObjectClass ObjectProperty { get; set; }
}
public class SomeObjectClass
{
public string SomeProperty { get; set; }
}

You can use reflection
//Other Imports...
using System.Reflection;
public PlaylistBox(ListBox In)
{
PropertyInfo[] properties = typeof(ListBox).GetProperties();
foreach (PropertyInfo p in properties)
if (p.CanRead && p.CanWrite)
p.SetMethod.Invoke(this, new object[] { p.GetMethod.Invoke(In, null) });
}
For .NET < 4.5, substitute calls to the GetMethod and SetMethod properties with calls to the GetGetMethod() and GetSetMethod() methods respectively.

Related

How to build a classmap at runtime for nested properties

Suppose I have data structures like this:
public class Foo
{
public Bar A {get;set;}
public Bar B {get;set;}
public int C {get;set;}
}
public class Bar
{
public int Value {get;set;}
}
and a CSV file with the contents
Column1,Column2,Column3
0,1,2
3,4,5
I would like to now map Column1 to A.Value and Column2 to B.Value and Column3 to C.
I'm restricted to runtime mapping.
For Column3 -> C, I can write
var type = typeof(Foo);
var customMap = Activator.CreateInstance(typeof(DefaultClassMap<>).MakeGenericType(type)) as ClassMap;
customMap.Map(type, type.GetProperty("C")).Name("Column3");
csv_reader.Context.RegisterClassMap(customMap);
How can I map columns 1 and 2?
Currently you can do this.
void Main()
{
var s = new StringBuilder();
s.Append("Column1,Column2,Column3\r\n");
s.Append("0,1,2\r\n");
s.Append("3,4,5\r\n");
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
};
using (var reader = new StringReader(s.ToString()))
using (var csv = new CsvReader(reader, config))
{
var fooType = typeof(Foo);
var barType = typeof(Bar);
var fooMapType = typeof(DefaultClassMap<>).MakeGenericType(fooType);
var barMapType = typeof(DefaultClassMap<>).MakeGenericType(barType);
var map = (ClassMap)ObjectResolver.Current.Resolve(fooMapType);
map.Map(fooType, fooType.GetProperty("C")).Name("Column3");
map.References(barMapType, fooType.GetProperty("A")).Data.Mapping.Map(barType, barType.GetProperty("Value")).Name("Column1");
map.References(barMapType, fooType.GetProperty("B")).Data.Mapping.Map(barType, barType.GetProperty("Value")).Name("Column2");
csv.Context.RegisterClassMap(map);
csv.GetRecords<Foo>().ToList().Dump();
}
}
private class Foo
{
public Bar A { get; set; }
public Bar B { get; set; }
public int C { get; set; }
}
public class Bar
{
public int Value { get; set; }
}
I'm looking into ways to make this easier for people that want to create maps at runtime.

How do i export child objects with EPPlus as Excel

I am using EPPlus to help me export data as excel. I am still learning to export data properly but somehow am stuck at a point where i am not able to export an object with child objects all flatted out.
ParentObject
public string A;
public string B;
public ChildObject ChildObject;
ChildObject
public string C;
public string D;
so i want my exported excel to look like
A B C D
aa1 bb1 cc1 dd1
aa2 bb2 cc2 dd2
aa3 bb3 cc3 dd3
This is how my current implementation looks like
public void CreateExcel(IEnumerable<T> dataCollection, string fullyQualifiedFileName, string worksheetName)
{
using (var package = new ExcelPackage(new FileInfo(fullyQualifiedFileName)))
{
var worksheet =
package.Workbook.Worksheets.FirstOrDefault(excelWorksheet => excelWorksheet.Name == worksheetName) ??
package.Workbook.Worksheets.Add(worksheetName);
var membersToInclude = typeof(T)
.GetMembers(BindingFlags.Instance | BindingFlags.Public)
.Where(p => Attribute.IsDefined(p, typeof(ExcelUtilityIgnoreAttribute)) == false
|| p.GetCustomAttribute<ExcelUtilityIgnoreAttribute>().IsIgnored == false)
.ToArray();
worksheet.Cells["A1"].LoadFromCollection(dataCollection, true, OfficeOpenXml.Table.TableStyles.None,
BindingFlags.Public, membersToInclude);
package.Save();
}
}
I tried using Microsoft generics using expando object but EPPlus wont work with generics, is there a way where in i can export objects with child objects ?
also: is there any other library that i could use ?
There is no native function that could do that. Hard to come up with something generic as it would require a great deal of assumption. What property type should be automatically exported vs what should be treated a child node and have ITS properties exported or expanded. But if you come up with that it is a basic tree traversal from there.
Below is something I adapted from a similar task. Here, I assume that anything that is a either a string or a data type without properties is considered an value type for exporting (int, double, etc.). But it is very easy to tweak as needed. I threw this together so it may not be fully optimized:
public static void ExportFlatExcel<T>(IEnumerable<T> dataCollection, FileInfo file, string worksheetName)
{
using (var package = new ExcelPackage(file))
{
var worksheet =
package.Workbook.Worksheets.FirstOrDefault(excelWorksheet => excelWorksheet.Name == worksheetName) ??
package.Workbook.Worksheets.Add(worksheetName);
const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public;
var props = typeof (T).GetProperties(flags);
//Map the properties to types
var rootTree = new Branch<PropertyInfo>(null);
var stack = new Stack<KeyValuePair<PropertyInfo, IBranch<PropertyInfo>>>(
props
.Reverse()
.Select(pi =>
new KeyValuePair<PropertyInfo, IBranch<PropertyInfo>>(
pi
, rootTree
)
)
);
//Do a non-recursive traversal of the properties
while (stack.Any())
{
var node = stack.Pop();
var prop = node.Key;
var branch = node.Value;
//Print strings
if (prop.PropertyType == typeof (string))
{
branch.AddNode(new Leaf<PropertyInfo>(prop));
continue;
}
//Values type do not have properties
var childProps = prop.PropertyType.GetProperties(flags);
if (!childProps.Any())
{
branch.AddNode(new Leaf<PropertyInfo>(prop));
continue;
}
//Add children to stack
var child = new Branch<PropertyInfo>(prop);
branch.AddNode(child);
childProps
.Reverse()
.ToList()
.ForEach(pi => stack
.Push(new KeyValuePair<PropertyInfo, IBranch<PropertyInfo>>(
pi
, child
)
)
);
}
//Go through the data
var rows = dataCollection.ToList();
for (var r = 0; r < rows.Count; r++)
{
var currRow = rows[r];
var col = 0;
foreach (var child in rootTree.Children)
{
var nodestack = new Stack<Tuple<INode, object>>();
nodestack.Push(new Tuple<INode, object>(child, currRow));
while (nodestack.Any())
{
var tuple = nodestack.Pop();
var node = tuple.Item1;
var currobj = tuple.Item2;
var branch = node as IBranch<PropertyInfo>;
if (branch != null)
{
currobj = branch.Data.GetValue(currobj, null);
branch
.Children
.Reverse()
.ToList()
.ForEach(cnode => nodestack.Push(
new Tuple<INode, object>(cnode, currobj)
));
continue;
}
var leaf = node as ILeaf<PropertyInfo>;
if (leaf == null)
continue;
worksheet.Cells[r + 2, ++col].Value = leaf.Data.GetValue(currobj, null);
if (r == 0)
worksheet.Cells[r + 1, col].Value = leaf.Data.Name;
}
}
}
package.Save();
package.Dispose();
}
}
So say you have these as a structure:
#region Classes
public class Parent
{
public string A { get; set; }
public Child1 Child1 { get; set; }
public string D { get; set; }
public int E { get; set; }
public Child2 Child2 { get; set; }
}
public class Child1
{
public string B { get; set; }
public string C { get; set; }
}
public class Child2
{
public Child1 Child1 { get; set; }
public string F { get; set; }
public string G { get; set; }
}
#endregion
#region Tree Nodes
public interface INode { }
public interface ILeaf<T> : INode
{
T Data { get; set; }
}
public interface IBranch<T> : ILeaf<T>
{
IList<INode> Children { get; }
void AddNode(INode node);
}
public class Leaf<T> : ILeaf<T>
{
public Leaf() { }
public Leaf(T data) { Data = data; }
public T Data { get; set; }
}
public class Branch<T> : IBranch<T>
{
public Branch(T data) { Data = data; }
public T Data { get; set; }
public IList<INode> Children { get; } = new List<INode>();
public void AddNode(INode node)
{
Children.Add(node);
}
}
#endregion
And this as a test:
[TestMethod]
public void ExportFlatTest()
{
var list = new List<Parent>();
for (var i = 0; i < 20; i++)
list.Add(new Parent
{
A = $"A-{i}",
D = $"D-{i}",
E = i*10,
Child1 = new Child1
{
B = $"Child1-B-{i}",
C = $"Child1-C-{i}",
},
Child2 = new Child2
{
F = $"F-{i}",
G = $"G-{i}",
Child1 = new Child1
{
B = $"Child2-Child1-B-{i}",
C = $"Child2-Child1-C-{i}",
}
}
});
var file = new FileInfo(#"c:\temp\flat.xlsx");
if (file.Exists)
file.Delete();
TestExtensions.ExportFlatExcel(
list
, file
, "Test1"
);
}
Will give you this:

Dynamically build an object from a strongly typed class using C#?

Currently, am adding the properties and values to the object manually like this example and sending to Dapper.SimpleCRUD to fetch data from Dapper Orm. This is the desired output I would like to achieve.
object whereCriteria = null;
whereCriteria = new
{
CountryId = 2,
CountryName = "Anywhere on Earth",
CountryCode = "AOE",
IsActive = true
};
The following class should build the object in the above mentioned format and return the ready-made object.
public static class WhereClauseBuilder
{
public static object BuildWhereClause(object model)
{
object whereObject = null;
var properties = GetProperties(model);
foreach (var property in properties)
{
var value = GetValue(property, model);
//Want to whereObject according to the property and value. Need help in this part!!!
}
return whereObject;
}
private static object GetValue(PropertyInfo property, object model)
{
return property.GetValue(model);
}
private static IEnumerable<PropertyInfo> GetProperties(object model)
{
return model.GetType().GetProperties();
}
}
This function WhereClauseBuilder.BuildWhereClause(object model) should return the object in expected format (mentiond above). Here is the implementation of how I would like to use.
public sealed class CountryModel
{
public int CountryId { get; set; }
public string CountryName { get; set; }
public string CountryCode { get; set; }
public bool IsActive { get; set; }
}
public class WhereClauseClass
{
public WhereClauseClass()
{
var model = new CountryModel()
{
CountryCode = "AOE",
CountryId = 2,
CountryName = "Anywhere on Earth",
IsActive = true
};
//Currently, won't return the correct object because the implementation is missing.
var whereClauseObject = WhereClauseBuilder.BuildWhereClause(model);
}
}
Maybe something like that:
private const string CodeTemplate = #"
namespace XXXX
{
public class Surrogate
{
##code##
}
}";
public static Type CreateSurrogate(IEnumerable<PropertyInfo> properties)
{
var compiler = new CSharpCodeProvider();
var compilerParameters = new CompilerParameters { GenerateInMemory = true };
foreach (var item in AppDomain.CurrentDomain.GetAssemblies().Where(x => !x.IsDynamic))
{
compilerParameters.ReferencedAssemblies.Add(item.Location);
}
var propertiesCode =
string.join("\n\n", from pi in properties
select "public " + pi.PropertyType.Name + " " + pi.Name + " { get; set; }");
var source = CodeTemplate.Replace("##code##", propertiesCode);
var compilerResult = compiler.CompileAssemblyFromSource(compilerParameters, source);
if (compilerResult.Errors.HasErrors)
{
throw new InvalidOperationException(string.Format("Surrogate compilation error: {0}", string.Join("\n", compilerResult.Errors.Cast<CompilerError>())));
}
return compilerResult.CompiledAssembly.GetTypes().First(x => x.Name == "Surrogate");
}
And now use it:
public static object BuildWhereClause(object model)
{
var properties = GetProperties(model);
var surrogateType = CreateSurrogate(properties);
var result = Activator.CreateInstance(surrogateType);
foreach (var property in properties)
{
var value = GetValue(property, model);
var targetProperty = surrogateType.GetProperty(property.Name);
targetProperty.SetValue(result, value, null);
}
return result;
}
I didn't compile that. It's only written here. Maybe there are some errors. :-)
EDIT:
To use ExpandoObject you can try this:
public static object BuildWhereClause(object model)
{
var properties = GetProperties(model);
var result = (IDictionary<string, object>)new ExpandoObject();
foreach (var property in properties)
{
var value = GetValue(property, model);
result.Add(property.Name, value);
}
return result;
}
But I don't know whether this will work for you.

Map list to existing list in Automapper using a key

Automapper easily handles mapping one list of object types to another list of different objects types, but is it possible to have it map to an existing list using an ID as a key?
I have not found better way than the following.
Here are source and destination.
public class Source
{
public int Id { get; set; }
public string Foo { get; set; }
}
public class Destination
{
public int Id { get; set; }
public string Foo { get; set; }
}
Define converter (You should change List<> to whatever type you are using).
public class CollectionConverter: ITypeConverter<List<Source>, List<Destination>>
{
public List<Destination> Convert(ResolutionContext context)
{
var destinationCollection = (List<Destination>)context.DestinationValue;
if(destinationCollection == null)
destinationCollection = new List<Destination>();
var sourceCollection = (List<Source>)context.SourceValue;
foreach(var source in sourceCollection)
{
Destination matchedDestination = null;
foreach(var destination in destinationCollection)
{
if(destination.Id == source.Id)
{
Mapper.Map(source, destination);
matchedDestination = destination;
break;
}
}
if(matchedDestination == null)
destinationCollection.Add(Mapper.Map<Destination>(source));
}
return destinationCollection;
}
}
And here is actual mapping configuration and example.
Mapper.CreateMap<Source,Destination>();
Mapper.CreateMap<List<Source>,List<Destination>>().ConvertUsing(new CollectionConverter());
var sourceCollection = new List<Source>
{
new Source{ Id = 1, Foo = "Match"},
new Source{ Id = 2, Foo = "DoesNotMatchWithDestination"}
};
var destinationCollection = new List<Destination>
{
new Destination{ Id = 1, Foo = "Match"},
new Destination{ Id = 3, Foo = "DoeNotMatchWithSource"}
};
var mergedCollection = Mapper.Map(sourceCollection, destinationCollection);
You should get the following result.
I found this article very useful and as such I thought I would feed back in my generic version of the type converter which you can use to select the property to match on from each object.
Using it all you need to do is:
// Example of usage
Mapper.CreateMap<UserModel, User>();
var converter = CollectionConverterWithIdentityMatching<UserModel, User>.Instance(model => model.Id, user => user.Id);
Mapper.CreateMap<List<UserModel>, List<User>>().ConvertUsing(converter);
//The actual converter
public class CollectionConverterWithIdentityMatching<TSource, TDestination> :
ITypeConverter<List<TSource>, List<TDestination>> where TDestination : class
{
private readonly Func<TSource, object> sourcePrimaryKeyExpression;
private readonly Func<TDestination, object> destinationPrimaryKeyExpression;
private CollectionConverterWithIdentityMatching(Expression<Func<TSource, object>> sourcePrimaryKey, Expression<Func<TDestination, object>> destinationPrimaryKey)
{
this.sourcePrimaryKeyExpression = sourcePrimaryKey.Compile();
this.destinationPrimaryKeyExpression = destinationPrimaryKey.Compile();
}
public static CollectionConverterWithIdentityMatching<TSource, TDestination>
Instance(Expression<Func<TSource, object>> sourcePrimaryKey, Expression<Func<TDestination, object>> destinationPrimaryKey)
{
return new CollectionConverterWithIdentityMatching<TSource, TDestination>(
sourcePrimaryKey, destinationPrimaryKey);
}
public List<TDestination> Convert(ResolutionContext context)
{
var destinationCollection = (List<TDestination>)context.DestinationValue ?? new List<TDestination>();
var sourceCollection = (List<TSource>)context.SourceValue;
foreach (var source in sourceCollection)
{
TDestination matchedDestination = default(TDestination);
foreach (var destination in destinationCollection)
{
var sourcePrimaryKey = GetPrimaryKey(source, this.sourcePrimaryKeyExpression);
var destinationPrimaryKey = GetPrimaryKey(destination, this.destinationPrimaryKeyExpression);
if (string.Equals(sourcePrimaryKey, destinationPrimaryKey, StringComparison.OrdinalIgnoreCase))
{
Mapper.Map(source, destination);
matchedDestination = destination;
break;
}
}
if (matchedDestination == null)
{
destinationCollection.Add(Mapper.Map<TDestination>(source));
}
}
return destinationCollection;
}
private string GetPrimaryKey<TObject>(object entity, Func<TObject, object> expression)
{
var tempId = expression.Invoke((TObject)entity);
var id = System.Convert.ToString(tempId);
return id;
}
}

How to Compare two class objecy with Reflection?

i try to compare two class comp1,comp2 i used method below: ComparerCollection(array_X, array_Y); but there are errors below. Arraylist generated from Ilist. how can i do that?
namespace GenericCollecitonComparer
{
class Program
{
static void Main(string[] args)
{
myClass comp1 = new myClass() { ID = 1, Name = "yusuf" };
myClass comp2 = new myClass() { ID = 1, Name = "yusufk" };
Comparer com = new Comparer(comp1, comp2);
Console.ReadKey();
}
}
public class Comparer
{
public Comparer(myClass x, myClass y)
{
PropertyInfo[] propInfo_X = x.GetType().GetProperties();
PropertyInfo[] propInfo_Y = y.GetType().GetProperties();
ArrayList array_X = new ArrayList();
ArrayList array_Y = new ArrayList();
foreach (PropertyInfo pi in propInfo_X)
array_X.Add(pi.GetValue(x, null));
foreach (PropertyInfo pi in propInfo_Y)
array_Y.Add(pi.GetValue(y, null));
// ComparerCollection(array_X, array_Y); --> Error below
}
public bool ComparerCollection<T>(IList<T> xlist, IList<T> ylist)
{
return xlist.SequenceEqual(ylist);
}
}
public class myClass
{
public int ID { get; set; }
public string Name { get; set; }
}
}
/* Error 1 The type arguments for method '
* GenericCollecitonComparer.Comparer.ComparerCollection<T>(System.Collections.Generic.IList<T>, System.Collections.Generic.IList<T>)'
* cannot be inferred from the usage. Try specifying the type arguments explicitly.
*
*/
The error you receive is due to the fact ArrayList is not a generic class. You can use List<object> instead to make it work.
An alternative implementation:
public class Comparer
{
public bool AreEqual { get; private set; }
public Comparer(myClass x, myClass y)
{
var xProperties = x.GetType().GetProperties();
var yProperties = y.GetType().GetProperties();
var xPropertiesValues = xProperties.Select(pi => pi.GetValue(x, null));
var yPropertiesValues = yProperties.Select(pi => pi.GetValue(y, null));
AreEqual = xPropertiesValues.SequenceEqual(yPropertiesValues);
}
}
And a usage example:
[Test]
public void UsageExample()
{
myClass comp1 = new myClass() { ID = 1, Name = "yusuf" };
myClass comp2 = new myClass() { ID = 1, Name = "yusufk" };
myClass comp3 = new myClass() { ID = 1, Name = "yusuf" };
Comparer comparerOf1And2 = new Comparer(comp1, comp2);
Assert.IsFalse(comparerOf1And2.AreEqual);
Comparer comparerOf1And3 = new Comparer(comp1, comp3);
Assert.IsTrue(comparerOf1And3.AreEqual);
}

Categories

Resources