Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 KB | None | 0 0
  1. private static object CombineConfigValues( object a, object b ) {
  2.     if( a.GetType() != b.GetType() ) {
  3.         throw new ModHelpersException( "Mismatched config value types." );
  4.     }
  5.     if( a == null ) {
  6.         return b;
  7.     }
  8.     if( b == null ) {
  9.         return a;
  10.     }
  11.     if( !(b is ICollection) ) {
  12.         return b;
  13.     }
  14.  
  15.     Type collectionType = b.GetType();
  16.  
  17.     List<object> rawCombinedList = ( (ICollection)a ).Cast<object>().ToList();
  18.     rawCombinedList.AddRange( ( (ICollection)b ).Cast<object>() );
  19.  
  20.     MethodInfo castMethod = rawCombinedList.GetType().GetMethod( "Cast" );
  21.     castMethod = castMethod.MakeGenericMethod( collectionType.GenericTypeArguments );
  22.  
  23.     var typedCombinedCollection = (IEnumerable)castMethod.Invoke( rawCombinedList, new object[] { } );
  24.  
  25.     return Activator.CreateInstance( collectionType, new object[] { typedCombinedCollection } );
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement