Click here to Skip to main content
15,895,709 members
Articles / Programming Languages / C# 4.0

Generic Type Mapping

Rate me:
Please Sign up or sign in to vote.
4.42/5 (10 votes)
18 Dec 2010CPOL3 min read 38.9K   526   20  
A utility to assign the values of one type to another.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenericTypeMapper
{
    public class ConcreteMappingProvider : IMappingProvider
    {
        public T Map<T>(IMappable input)
        {
            // TypeB cannot be cast directly from TypeA
            // but it can be cast from object.
            object result = map(input as TypeA);
            return (T)result;
        }

        private TypeB map(TypeA input)
        {
            TypeB result = new TypeB();
            result.StringX = input.StringA;
            result.intY = input.IntB;
            result.StringZ = input.GuidC.ToString();            
            return result;
        }
       
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Ireland Ireland
Zzzzz....

Comments and Discussions