Click here to Skip to main content
15,913,709 members
Home / Discussions / C#
   

C#

 
GeneralRe: Cheque Print Software - source code Pin
GNPrinting5-Apr-11 19:12
GNPrinting5-Apr-11 19:12 
GeneralRe: Cheque Print source code Pin
zuber ahmed17-May-14 3:14
zuber ahmed17-May-14 3:14 
QuestionLoading or creating a Application depending on different versions Pin
vijaylumar13-Sep-09 3:30
vijaylumar13-Sep-09 3:30 
AnswerRe: Loading or creating a Application depending on different versions Pin
Eddy Vluggen13-Sep-09 3:40
professionalEddy Vluggen13-Sep-09 3:40 
QuestionCopy Class to Another Class - Cast Problem Pin
dataminers13-Sep-09 3:12
dataminers13-Sep-09 3:12 
AnswerRe: Copy Class to Another Class - Cast Problem Pin
N a v a n e e t h13-Sep-09 3:25
N a v a n e e t h13-Sep-09 3:25 
GeneralRe: Copy Class to Another Class - Cast Problem Pin
dataminers13-Sep-09 3:50
dataminers13-Sep-09 3:50 
GeneralRe: Copy Class to Another Class - Cast Problem Pin
N a v a n e e t h13-Sep-09 17:49
N a v a n e e t h13-Sep-09 17:49 
You can use reflection and copy the values from source to target for all the matching members. You should do something like,

1) DeepCopy method should take two types, source and target and return the target type
2) Get all fields including private fields from source.
3) Look for the same field name in target.
4) Assign source field value to target field value.
5) Continue until you finish all members.

Here is a generic DeepCopy implementation.
C#
TSource Clone<TSource>(TSource source)
{
    TSource cloned = default(TSource);
    using (var stream = new MemoryStream())
    {
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(stream, source);
        stream.Position = 0;
        cloned = (TSource)formatter.Deserialize(stream);
    }
    return cloned;
}

TTarget DeepCopy<TSource,TTarget>(TSource source) where TTarget: new()
{
    // cloning the source
    TSource cloned = Clone(source);

    Type targetType = typeof(TTarget);
    TTarget target = new TTarget();

    BindingFlags memberSearchFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
    BindingFlags fieldSearchFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField;

    MemberInfo[] targetMembers = targetType.GetMembers(fieldSearchFlags);

    foreach (MemberInfo sourceMember in typeof(TSource).GetMembers(memberSearchFlags))
    {
        if(sourceMember.MemberType == MemberTypes.Field)
        {
            FieldInfo sourceField = (FieldInfo) sourceMember;
            FieldInfo targetField = (FieldInfo) targetMembers.SingleOrDefault(m => m.Name == sourceField.Name);

            if (targetField != null)
                targetField.SetValue(target, sourceField.GetValue(source));
        }
    }
    return target;
}
Use this like.
AkdPerson akd = new AkdPerson() { PersonID = 10, PersonName = "Chuck Norris" };
XPOPerson xpo = DeepCopy<AkdPerson,XPOPerson>(akd);
Note : This will copy only the members whose name matches. This will leave all other members uninitialized and you should take care about initializing them before you use them.

Smile | :)


GeneralRe: Copy Class to Another Class - Cast Problem Pin
dataminers14-Sep-09 23:33
dataminers14-Sep-09 23:33 
AnswerRe: Copy Class to Another Class - Cast Problem Pin
Eddy Vluggen13-Sep-09 3:42
professionalEddy Vluggen13-Sep-09 3:42 
GeneralRe: Copy Class to Another Class - Cast Problem Pin
dataminers13-Sep-09 3:51
dataminers13-Sep-09 3:51 
GeneralRe: Copy Class to Another Class - Cast Problem Pin
Eddy Vluggen13-Sep-09 3:54
professionalEddy Vluggen13-Sep-09 3:54 
GeneralRe: Copy Class to Another Class - Cast Problem Pin
dataminers13-Sep-09 4:01
dataminers13-Sep-09 4:01 
GeneralRe: Copy Class to Another Class - Cast Problem Pin
J4amieC13-Sep-09 4:53
J4amieC13-Sep-09 4:53 
AnswerRe: Copy Class to Another Class - Cast Problem Pin
J4amieC13-Sep-09 4:55
J4amieC13-Sep-09 4:55 
AnswerRe: Copy Class to Another Class - Cast Problem Pin
PIEBALDconsult13-Sep-09 5:10
mvePIEBALDconsult13-Sep-09 5:10 
QuestionThe remote certificate is invalid according to the validation procedure. Pin
khosnur13-Sep-09 2:17
khosnur13-Sep-09 2:17 
Questionpop3 secure mail download using C# Pin
khosnur13-Sep-09 0:51
khosnur13-Sep-09 0:51 
AnswerRe: pop3 secure mail download using C# Pin
Richard MacCutchan13-Sep-09 1:31
mveRichard MacCutchan13-Sep-09 1:31 
GeneralRe: pop3 secure mail download using C# Pin
khosnur13-Sep-09 2:22
khosnur13-Sep-09 2:22 
GeneralRe: pop3 secure mail download using C# Pin
Richard MacCutchan13-Sep-09 3:18
mveRichard MacCutchan13-Sep-09 3:18 
AnswerRe: pop3 secure mail download using C# Pin
dan!sh 13-Sep-09 1:42
professional dan!sh 13-Sep-09 1:42 
GeneralRe: pop3 secure mail download using C# Pin
khosnur13-Sep-09 2:28
khosnur13-Sep-09 2:28 
GeneralRe: pop3 secure mail download using C# Pin
dan!sh 13-Sep-09 3:53
professional dan!sh 13-Sep-09 3:53 
QuestionRetreive Email & Attachments [modified] Pin
kdwarak13-Sep-09 0:16
kdwarak13-Sep-09 0:16 

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.