Click here to Skip to main content
15,886,137 members
Home / Discussions / C#
   

C#

 
AnswerRe: Enum Question Pin
Richard Deeming18-Dec-12 8:53
mveRichard Deeming18-Dec-12 8:53 
GeneralRe: Enum Question Pin
SledgeHammer0118-Dec-12 10:37
SledgeHammer0118-Dec-12 10:37 
AnswerRe: Enum Question Pin
DaveyM6918-Dec-12 9:37
professionalDaveyM6918-Dec-12 9:37 
GeneralRe: Enum Question Pin
PIEBALDconsult18-Dec-12 10:43
mvePIEBALDconsult18-Dec-12 10:43 
QuestionVsIntegrationPackage: Debugging does not reload .vsct file Pin
Member 802402518-Dec-12 4:16
Member 802402518-Dec-12 4:16 
QuestionHow to shwo the values in radiobuttons and labels which are reside inside a single column of gridview Pin
Arun kumar Gautam18-Dec-12 1:47
Arun kumar Gautam18-Dec-12 1:47 
AnswerRe: How to shwo the values in radiobuttons and labels which are reside inside a single column of gridview Pin
Richard MacCutchan18-Dec-12 7:11
mveRichard MacCutchan18-Dec-12 7:11 
QuestionReflection question (for <= .NET 3.5) Pin
MarkB77718-Dec-12 0:24
MarkB77718-Dec-12 0:24 
Hey guys,

Long story short:

I have a number of classes that are structured like this:
C#
class ClassA
{
    [WantToCopy]
    public Property1 { get; set; }
    public Property2 { get; set; } // ...
}

class ClassB : ClassA
{
    public Property1 { get; set; }
    public Property2 { get; set; } // ...
}

class ClassC
{
    public ClassB { get; set; }
    public Property1 { get; set; }
    public Property2 { get; set; } // ...
}


I am trying copy the ClassA values (marked with a specific attribute) from an instance of ClassB to a new instance of ClassA.

This is what I am doing:

C#
T resp = GetClassC(); // get a pretend instance of ClassC

var types = resp.GetType().GetProperties();
foreach (var type in types)
{
    // I use this to figure out if the field in ClassC == ClassB
    if(type.PropertyType.IsSubclassOf(this.GetType()))
    {
        // I use this custom method to get the properties in ClassA that have the 
        // attribute mentioned above
        List<PropertyInfo> fields = GetFields(type.PropertyType);
        foreach (var field in fields)
        {
            // here is the problem!
            var value = field.GetValue(resp , null); // resp is ClassC
            field.SetValue(this, value , null); // 'this' is ClassA
        }
    }
}


The problem is I get a TargetException when I try and get a ClassA value from a ClassC instance (which makes sense).

ClassC is a generic type. I don't know anything about the ClassB member.

Does anyone have any tricks that could help me out here? Or will I have to use an interface?

P.S I am keen to keep this code to <= .NET 3.5 if possible.

Thank you,
Mark
Mark Brock
"We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen

AnswerRe: Reflection question (for <= .NET 3.5) Pin
Richard Deeming18-Dec-12 1:39
mveRichard Deeming18-Dec-12 1:39 
GeneralRe: Reflection question (for <= .NET 3.5) Pin
MarkB77718-Dec-12 8:50
MarkB77718-Dec-12 8:50 
AnswerRe: Reflection question (for <= .NET 3.5) Pin
PIEBALDconsult18-Dec-12 5:17
mvePIEBALDconsult18-Dec-12 5:17 
GeneralRe: Reflection question (for <= .NET 3.5) Pin
MarkB77718-Dec-12 8:53
MarkB77718-Dec-12 8:53 
Questioncrystalreport Pin
mehdi jan17-Dec-12 19:26
mehdi jan17-Dec-12 19:26 
AnswerRe: crystalreport Pin
Mycroft Holmes17-Dec-12 21:22
professionalMycroft Holmes17-Dec-12 21:22 
QuestionFTP Pin
dineshvalluru1116-Dec-12 20:39
dineshvalluru1116-Dec-12 20:39 
AnswerRe: FTP Pin
Sivaraman Dhamodharan16-Dec-12 20:44
Sivaraman Dhamodharan16-Dec-12 20:44 
GeneralRe: FTP Pin
dineshvalluru1116-Dec-12 21:36
dineshvalluru1116-Dec-12 21:36 
GeneralRe: FTP Pin
Richard MacCutchan16-Dec-12 22:08
mveRichard MacCutchan16-Dec-12 22:08 
GeneralRe: FTP Pin
Paul Conrad17-Dec-12 5:59
professionalPaul Conrad17-Dec-12 5:59 
GeneralRe: FTP Pin
Richard MacCutchan17-Dec-12 6:00
mveRichard MacCutchan17-Dec-12 6:00 
GeneralRe: FTP Pin
Marco Bertschi18-Dec-12 1:34
protectorMarco Bertschi18-Dec-12 1:34 
QuestionDataGridView Control Pin
C#_Programmer16-Dec-12 19:51
C#_Programmer16-Dec-12 19:51 
AnswerRe: DataGridView Control Pin
Ingo16-Dec-12 23:36
Ingo16-Dec-12 23:36 
GeneralRe: DataGridView Control Pin
C#_Programmer17-Dec-12 1:26
C#_Programmer17-Dec-12 1:26 
GeneralRe: DataGridView Control Pin
Dave Kreskowiak17-Dec-12 3:13
mveDave Kreskowiak17-Dec-12 3:13 

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.