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 8431233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:47:57+00:00 2026-06-10T05:47:57+00:00

How to pass by ref different types of objects that have the same properties

  • 0

How to pass by ref different types of objects that have the same properties inside and populate them without interface.

In my app there are totally different types which have some properties in common.
Let’s say this properties are double arrays.

double[] samples;

Now I have to populate these samples for 20 objects.
I don’t have any access to the class definition of this object, so I can’t make interface, or make them inherit from a base class.

How can use one method which I call and this method to populate all my properties.

I want to have one method like this:

private static void FillSamples(ref WhoKnowsWhat dataType, MyObject theSamples)
{

for (int i = 0; i < sampleCount; i++)
                        {
                            dataType.SampleLength[i] = MyObject.X[i];
                            dataType.SampleValue[i]  = MyObject.y[i];
                        }
}

And call this with totally different types.

FillSamples(ref BigStruct.OneTypeMine, theSamples);
FillSamples(ref BigStruct.AnotherTypeMine, theSamples);
FillSamples(ref BigStruct.HisType12345, theSamples);

Then the big struct should have these samples filled in the end.

Is there a way in C#?

Thanks!

  • 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. Editorial Team
    Editorial Team
    2026-06-10T05:47:59+00:00Added an answer on June 10, 2026 at 5:47 am

    You can use the dynamic keyword:

    private static void FillSamples(dynamic dataType, MyObject theSamples)
    {
        for (int i = 0; i < sampleCount; i++)
        {
            dataType.SampleLength[i] = MyObject.X[i];
            dataType.SampleValue[i]  = MyObject.y[i];
        }
    }
    

    Edit:

    Using reflection (if you don’t use .Net 4.0 or higher):

    private static void FillSamples(object dataType, MyObject theSamples)
    {
        Type t = dataType.GetType();
        var px = t.GetProperty("SampleLength");
        var py = t.GetProperty("SampleValue");
    
        for (int i = 0; i < sampleCount; i++)
        {
            px.SetValue(dataType, MyObject.X[i], null);
            py.SetValue(dataType, MyObject.Y[i], null);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a delegate that modifies an object. I pass an object to the
I have a function that takes a ref parameter and would like to use
I've this question about pass some instances by ref or not: here is my
I want to pass an ImageIcon instance by value as we know that by
I have an incredibly simple query (table type InnoDb) and EXPLAIN says that MySQL
I want to output two different views (one as a string that will be
Should I use ref to pass a list variable by reference to a method?
//powershell code $quiotent = divRemainderQuiotent ($a) ($b) ([ref] $remainder) # I need to pass
I am using WMI to create different types of DNS records but am having
I have a problem with time measuring that's really bothering me. I am executing

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.