Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm at it again!<br> Here's the solution to all your <code>List&lt;T&gt;</code> and <code>Enumerable&lt;T&gt;</code> conversion problems. ~150 lines of code<br> Just be sure to define at least one explicit or implicit conversion operator for the input/output types involved (if one does not exist), as you should be doing anyway!</p> <pre><code>using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace System.Collections.Generic //purposely in same namespace as List&lt;T&gt;,IEnumerable&lt;T&gt;, so extension methods are available with them { public static class Enumerable { public static List&lt;TOutput&gt; ConvertAll&lt;TInput,TOutput&gt;( this IEnumerable&lt;TInput&gt; input ) { return BuildConvertedList&lt;TInput,TOutput&gt;( input, GetConverterDelegate&lt;TInput,TOutput&gt;() ); } public static IEnumerable&lt;TOutput&gt; ConvertAll&lt;TInput,TOutput&gt;( this IEnumerable&lt;TInput&gt; input, bool lazy ) { if (lazy) return new LazyConverter&lt;TInput,TOutput&gt;( input, GetConverterDelegate&lt;TInput,TOutput&gt;() ); return BuildConvertedList&lt;TInput,TOutput&gt;( input, GetConverterDelegate&lt;TInput,TOutput&gt;() ); } public static List&lt;TOutput&gt; ConvertAll&lt;TInput,TOutput&gt;( this IEnumerable&lt;TInput&gt; input, Converter&lt;TInput, TOutput&gt; converter ) { return BuildConvertedList&lt;TInput,TOutput&gt;( input, converter ); } public static List&lt;TOutput&gt; ConvertAll&lt;TInput, TOutput&gt;( this List&lt;TInput&gt; input ) { Converter&lt;TInput, TOutput&gt; converter = GetConverterDelegate&lt;TInput,TOutput&gt;(); return input.ConvertAll&lt;TOutput&gt;( converter ); } public static IEnumerable&lt;TOutput&gt; ConvertAll&lt;TInput, TOutput&gt;( this List&lt;TInput&gt; input, Converter&lt;TInput, TOutput&gt; converter, bool lazy ) { if (lazy) return new LazyConverter&lt;TInput, TOutput&gt;( input, converter ); return input.ConvertAll&lt;TOutput&gt;( converter ); } public static List&lt;TOutput&gt; ConvertAll&lt;TInput, TOutput&gt;( this List&lt;TInput&gt; input, Converter&lt;TInput, TOutput&gt; converter ) { return input.ConvertAll&lt;TOutput&gt;( converter ); } //Used to manually build converted list when input is IEnumerable, since it doesn't have the ConvertAll method like the List does private static List&lt;TOutput&gt; BuildConvertedList&lt;TInput,TOutput&gt;( IEnumerable&lt;TInput&gt; input, Converter&lt;TInput, TOutput&gt; converter ){ List&lt;TOutput&gt; output = new List&lt;TOutput&gt;(); foreach (TInput input_item in input) output.Add( converter( input_item ) ); return output; } private sealed class LazyConverter&lt;TInput, TOutput&gt;: IEnumerable&lt;TOutput&gt;, IEnumerator&lt;TOutput&gt; { private readonly IEnumerable&lt;TInput&gt; input; private readonly Converter&lt;TInput, TOutput&gt; converter; private readonly IEnumerator&lt;TInput&gt; input_enumerator; public LazyConverter( IEnumerable&lt;TInput&gt; input, Converter&lt;TInput, TOutput&gt; converter ) { this.input = input; this.converter = converter; this.input_enumerator = input.GetEnumerator(); } public IEnumerator&lt;TOutput&gt; GetEnumerator() {return this;} //IEnumerable&lt;TOutput&gt; Member IEnumerator IEnumerable.GetEnumerator() {return this;} //IEnumerable Member public void Dispose() {input_enumerator.Dispose();} //IDisposable Member public TOutput Current {get {return converter.Invoke( input_enumerator.Current );}} //IEnumerator&lt;TOutput&gt; Member object IEnumerator.Current {get {return Current;}} //IEnumerator Member public bool MoveNext() {return input_enumerator.MoveNext();} //IEnumerator Member public void Reset() {input_enumerator.Reset();} //IEnumerator Member } private sealed class TypeConversionPair: IEquatable&lt;TypeConversionPair&gt; { public readonly Type source_type; public readonly Type target_type; private readonly int hashcode; public TypeConversionPair( Type source_type, Type target_type ) { this.source_type = source_type; this.target_type = target_type; //precalc/store hash, since object is immutable; add one to source hash so reversing the source and target still produces unique hash hashcode = (source_type.GetHashCode() + 1) ^ target_type.GetHashCode(); } public static bool operator ==( TypeConversionPair x, TypeConversionPair y ) { if ((object)x != null) return x.Equals( y ); if ((object)y != null) return y.Equals( x ); return true; //x and y are both null, cast to object above ensures reference equality comparison } public static bool operator !=( TypeConversionPair x, TypeConversionPair y ) { if ((object)x != null) return !x.Equals( y ); if ((object)y != null) return !y.Equals( x ); return false; //x and y are both null, cast to object above ensures reference equality comparison } //TypeConversionPairs are equal when their source and target types are equal public bool Equals( TypeConversionPair other ) { if ((object)other == null) return false; //cast to object ensures reference equality comparison return source_type == other.source_type &amp;&amp; target_type == other.target_type; } public override bool Equals( object obj ) { TypeConversionPair other = obj as TypeConversionPair; if ((object)other != null) return Equals( other ); //call IEqualityComparer&lt;TypeConversionPair&gt; implementation if obj type is TypeConversionPair return false; //obj is null or is not of type TypeConversionPair; Equals shall not throw errors! } public override int GetHashCode() {return hashcode;} //assigned in constructor; object is immutable } private static readonly Dictionary&lt;TypeConversionPair,Delegate&gt; conversion_op_cache = new Dictionary&lt;TypeConversionPair,Delegate&gt;(); //Uses reflection to find and create a Converter&lt;TInput, TOutput&gt; delegate for the given types. //Once a delegate is obtained, it is cached, so further requests for the delegate do not use reflection* //(*the typeof operator is used twice to look up the type pairs in the cache) public static Converter&lt;TInput, TOutput&gt; GetConverterDelegate&lt;TInput, TOutput&gt;() { Delegate converter; TypeConversionPair type_pair = new TypeConversionPair( typeof(TInput), typeof(TOutput) ); //Attempt to quickly find a cached conversion delegate. lock (conversion_op_cache) //synchronize with concurrent calls to Add if (conversion_op_cache.TryGetValue( type_pair, out converter )) return (Converter&lt;TInput, TOutput&gt;)converter; //Get potential conversion operators (target-type methods are ordered first) MethodInfo[][] conversion_op_sets = new MethodInfo[2][] { type_pair.target_type.GetMethods( BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy ), type_pair.source_type.GetMethods( BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy ) }; //Find appropriate conversion operator, //favoring operators on target type in case functionally equivalent operators exist, //since the target type's conversion operator may have access to an appropriate constructor //or a common instance cache (i.e. immutable objects may be cached and reused). for (int s = 0; s &lt; conversion_op_sets.Length; s++) { MethodInfo[] conversion_ops = conversion_op_sets[s]; for (int m = 0; m &lt; conversion_ops.Length; m++) { MethodInfo mi = conversion_ops[m]; if ((mi.Name == "op_Explicit" || mi.Name == "op_Implicit") &amp;&amp; mi.ReturnType == type_pair.target_type &amp;&amp; mi.GetParameters()[0].ParameterType.IsAssignableFrom( type_pair.source_type )) //Assuming op_Explicit and op_Implicit always have exactly one parameter. { converter = Delegate.CreateDelegate( typeof(Converter&lt;TInput, TOutput&gt;), mi ); lock (conversion_op_cache) //synchronize with concurrent calls to TryGetValue conversion_op_cache.Add( type_pair, converter ); //Cache the conversion operator reference for future use. return (Converter&lt;TInput, TOutput&gt;)converter; } } } return (TInput x) =&gt; ((TOutput)Convert.ChangeType( x, typeof(TOutput) )); //this works well in the absence of conversion operators for types that implement IConvertible //throw new InvalidCastException( "Could not find conversion operator to convert " + type_pair.source_type.FullName + " to " + type_pair.target_type.FullName + "." ); } } } </code></pre> <p><strong>Sample use:</strong></p> <pre><code>using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { List&lt;string&gt; list = new List&lt;string&gt;(new string[] { "abcde", "abcd", "abc"/*will break length constraint*/, "ab", "a" }); //Uncomment line below to see non-lazy behavior. All items converted before method returns, and will fail on third item, which breaks the length constraint. //List&lt;ConstrainedString&gt; constrained_list = list.ConvertAll&lt;string,ConstrainedString&gt;(); IEnumerable&lt;ConstrainedString&gt; constrained_list = list.ConvertAll&lt;string,ConstrainedString&gt;( true ); //lazy conversion; conversion is not attempted until that item is read foreach (ConstrainedString constrained_string in constrained_list) //will not fail until the third list item is read/converted System.Console.WriteLine( constrained_string.ToString() ); } public class ConstrainedString { private readonly string value; public ConstrainedString( string value ){this.value = Constrain(value);} public string Constrain( string value ) { if (value.Length &gt; 3) return value; throw new ArgumentException("String length must be &gt; 3!"); } public static explicit operator ConstrainedString( string value ){return new ConstrainedString( value );} public override string ToString() {return value;} } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload