Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static object CombineConfigValues( object a, object b ) {
- if( a.GetType() != b.GetType() ) {
- throw new ModHelpersException( "Mismatched config value types." );
- }
- if( a == null ) {
- return b;
- }
- if( b == null ) {
- return a;
- }
- if( !(b is ICollection) ) {
- return b;
- }
- Type collectionType = b.GetType();
- List<object> rawCombinedList = ( (ICollection)a ).Cast<object>().ToList();
- rawCombinedList.AddRange( ( (ICollection)b ).Cast<object>() );
- object sample = rawCombinedList.FirstOrDefault();
- if( sample == null ) {
- return b;
- }
- MethodInfo castMethod = rawCombinedList.GetType().GetMethod( "Cast" );
- castMethod = castMethod.MakeGenericMethod( sample.GetType() );
- var typedCombinedCollection = (IEnumerable)castMethod.Invoke( rawCombinedList, new object[] { } );
- return Activator.CreateInstance( collectionType, new object[] { typedCombinedCollection } );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement