Click here to Skip to main content
15,908,172 members
Home / Discussions / C#
   

C#

 
GeneralRe: HttpWebRequest ? Pin
Vasudevan Deepak Kumar18-Dec-07 4:12
Vasudevan Deepak Kumar18-Dec-07 4:12 
GeneralRe: HttpWebRequest ? Pin
cd_dvd18-Dec-07 4:31
cd_dvd18-Dec-07 4:31 
GeneralRuntime type casting Pin
Skippums18-Dec-07 3:23
Skippums18-Dec-07 3:23 
GeneralRe: Runtime type casting Pin
CKnig18-Dec-07 3:29
CKnig18-Dec-07 3:29 
GeneralRe: Runtime type casting Pin
CKnig18-Dec-07 3:32
CKnig18-Dec-07 3:32 
GeneralRe: Runtime type casting Pin
Skippums18-Dec-07 6:37
Skippums18-Dec-07 6:37 
GeneralRe: Runtime type casting Pin
m@u18-Dec-07 3:35
m@u18-Dec-07 3:35 
GeneralRe: Runtime type casting Pin
Skippums18-Dec-07 5:56
Skippums18-Dec-07 5:56 
Thanks! My final code for the method in question is copied below:
private static readonly string[] s_CastOps = new string[] { "op_Explicit", "op_Implicit" };

public static T Convert<T, U>(U other) where T : new() {
    T rval = new T();
    foreach (PropertyInfo tPropInfo in typeof(T).GetProperties()) {
        if (tPropInfo.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), true).Length > 0)
            continue;
        PropertyInfo uPropInfo = typeof(U).GetProperty(tPropInfo.Name);
        if (uPropInfo != null && tPropInfo.CanWrite && uPropInfo.CanRead
            && uPropInfo.GetIndexParameters().Length == 0
            && tPropInfo.GetIndexParameters().Length == 0) {
            Type tPropType = tPropInfo.PropertyType;
            Type uPropType = uPropInfo.PropertyType;
            if (tPropType.Equals(uPropType)) {
                tPropInfo.SetValue(rval, uPropInfo.GetValue(other, null), null);
                continue;
            }
            Type[] uPropTypeArray = new Type[] { uPropType };
            object[] uValue = new object[] { uPropInfo.GetValue(other, null) };
            ConstructorInfo tConstruct = tPropType.GetConstructor(uPropTypeArray);
            if (tConstruct != null) {
                tPropInfo.SetValue(rval, tConstruct.Invoke(uValue), null);
                continue;
            }
            foreach (Type propType in new Type[] { tPropType, uPropType }) {
                foreach (string castOp in s_CastOps) {
                    MethodInfo ConvertUtoT = propType.GetMethod(castOp, uPropTypeArray);
                    if (ConvertUtoT != null && tPropType.Equals(ConvertUtoT.ReturnType)) {
                        tPropInfo.SetValue(rval, ConvertUtoT.Invoke(null, uValue), null);
                        goto _endConvert;
                    }
                }
            }
_endConvert:
            continue;
        }
    }
    return rval;
}
Basically, I go through the list of properties on both types. If both have a property with the same name, I try to copy the value from one to the other. If they are the same type, I simply set one to the other. Otherwise, I check if the result type has a constructor that takes the source type as a parameter. If so, I call the constructor. If not, then I look for a typecast definition between the two types (either explicit or implicit). If found, I cast the value of the source property to the type of the target property. This works great to copy objects with the same (or similar) properties into different types. Thanks for the help!
Sounds like somebody's got a case of the Mondays

-Jeff

GeneralDetermine User Role (Vista) Pin
Stevo Z18-Dec-07 3:10
Stevo Z18-Dec-07 3:10 
GeneralRe: Determine User Role (Vista) Pin
Peter Walburn26-Feb-10 4:38
Peter Walburn26-Feb-10 4:38 
GeneralTAPI 2 Pin
baerten18-Dec-07 3:09
baerten18-Dec-07 3:09 
GeneralConvert to int Pin
eyeseetee18-Dec-07 1:55
eyeseetee18-Dec-07 1:55 
GeneralRe: Convert to int Pin
J4amieC18-Dec-07 1:59
J4amieC18-Dec-07 1:59 
GeneralRe: Convert to int Pin
Pete O'Hanlon18-Dec-07 2:28
mvePete O'Hanlon18-Dec-07 2:28 
GeneralRe: Convert to int Pin
eyeseetee18-Dec-07 3:00
eyeseetee18-Dec-07 3:00 
GeneralRe: Convert to int Pin
Vasudevan Deepak Kumar18-Dec-07 3:01
Vasudevan Deepak Kumar18-Dec-07 3:01 
GeneralRe: Convert to int Pin
eyeseetee18-Dec-07 3:13
eyeseetee18-Dec-07 3:13 
GeneralRe: Convert to int Pin
eyeseetee18-Dec-07 3:18
eyeseetee18-Dec-07 3:18 
GeneralUsing WebService saving the files to client system using its Internet IP address Pin
ag4667718-Dec-07 0:30
ag4667718-Dec-07 0:30 
Questioncan we hide focus from grid cell Pin
amit_8318-Dec-07 0:20
amit_8318-Dec-07 0:20 
AnswerRe: can we hide focus from grid cell Pin
Vasudevan Deepak Kumar18-Dec-07 3:00
Vasudevan Deepak Kumar18-Dec-07 3:00 
Questionredirecting Pin
liatma17-Dec-07 23:57
liatma17-Dec-07 23:57 
GeneralRe: redirecting Pin
Paul Conrad22-Dec-07 9:21
professionalPaul Conrad22-Dec-07 9:21 
Questionhow to deactivate grid cell Pin
amit_8317-Dec-07 23:51
amit_8317-Dec-07 23:51 
QuestionWhat this code is doing Pin
Hum Dum17-Dec-07 23:38
Hum Dum17-Dec-07 23:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.