Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 130989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:02:05+00:00 2026-05-11T06:02:05+00:00

I currently have this type of code: private void FillObject(Object MainObject, Foo Arg1, Bar

  • 0

I currently have this type of code:

private void FillObject(Object MainObject, Foo Arg1, Bar Arg2) {     if (MainObject is SomeClassType1)     {         SomeClassType1 HelpObject = (SomeClassType1)MainObject;         HelpObject.Property1 = Arg1;         HelpObject.Property2 = Arg2;     }     else if (MainObject is SomeClassType2)     {         SomeClassType2 HelpObject = (SomeClassType2)MainObject;         HelpObject.Property1 = Arg1;         HelpObject.Property2 = Arg2;     } } 

Assuming that SomeClassType1 and SomeClassType2 have the same set of properties that I want to assign (although they may differ in other ones), is it possible to dynamically cast MainObject to the appropriate type and then assign the value, without duplicating the code? This is what I would like to see in the end:

private void FillObject(Object MainObject, Foo Arg1, Bar Arg2) {     Type DynamicType = null;      if (MainObject is SomeClassType1)     {         DynamicType = typeof(SomeClassType1);     }     else if (MainObject is SomeClassType2)     {         DynamicType = typeof(SomeClassType2);     }      DynamicType HelpObject = (DynamicType)MainObject;     HelpObject.Property1 = Arg1;     HelpObject.Property2 = Arg2; } 

And obviously C# complains about not being able to find DynamicType:

The type or namespace name ‘DynamicType’ could not be found (are you missing a using directive or an assembly reference?)

Is something like this possible in C# 2.0? If it’s more messy than my current code, than I see no point in doing this, but I’m very interested to find out. Thanks!

EDIT: Just to clarify, I perfectly understand that implementing an interface is the most appropriate and probably correct solution. That said, I’m more interested in seeing how to I could do it without implementing an interface. Thanks for great replies!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:02:06+00:00Added an answer on May 11, 2026 at 6:02 am

    It looks like both of the types you care about implement the same two properties. In that case, what you want to do is define an interface for those properties:

    public interface IMyInterface {    public Foo Property1 {get; set;}    public Bar Property2 {get;set;} } 

    Then, make sure each of your classes tell the compiler that they implement that new interface. Finally, use a generic method with a type argument that is constrained to that interace:

    private void FillObject<T>(T MainObject, Foo Arg1, Bar Arg2)      where T : IMyInterface {     MainObject.Property1 = Arg1;     MainObject.Property2 = Arg2; } 

    Note that even with the extra code to declare the interface, these snippets still end up shorter than either one of the snippets you posted in the question, and this code is much easier to extend if the number of types you care about increases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 175k
  • Answers 175k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You have three choices: live with it; use something like… May 12, 2026 at 3:00 pm
  • Editorial Team
    Editorial Team added an answer There is a getline defined for strings: std::string line; std::getline(std::cin,… May 12, 2026 at 3:00 pm
  • Editorial Team
    Editorial Team added an answer Try: SELECT id, topic FROM topics WHERE id IN (SELECT… May 12, 2026 at 3:00 pm

Related Questions

I have an array of different type objects and I use a BinaryWriter to
i am currently working with MEF and facing some problems what i want is
I'm currently designing a list widget to add to my widget engine. Since every
Why can't you pass an anonymous method as a parameter to the BeginInvoke method?

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.