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

  • Home
  • SEARCH
  • 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 757181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:19:57+00:00 2026-05-14T15:19:57+00:00

I am writing a C# wrapper for a 3rd party library that reads both

  • 0

I am writing a C# wrapper for a 3rd party library that reads both single values and arrays from a hardware device, but always returns an object[] array even for one value. This requires repeated calls to object[0] when I’d like the end user to be able to use generics to receive either an array or single value.

I want to use generics so the callee can use the wrapper in the following ways:

MyWrapper<float> mw = new MyWrapper<float>( ... );
float value = mw.Value; //should return float;

MyWrapper<float[]> mw = new MyWrapper<float[]>( ... );
float[] values = mw.Value; //should return float[];

In MyWrapper I have the Value property currently as the following:

public T Value
{
   get
   {
      if(_wrappedObject.Values.Length > 1)
         return (T)_wrappedObject.Value; //T could be float[]. this doesn't compile.
      else
         return (T)_wrappedObject.Values[0]; //T could be float. this compiles.
   }
}

I get a compile error in the first case:

Cannot convert type
‘object[]’ to ‘T’

If I change MyWrapper.Value to T[] I receive:

Cannot convert type
‘object[]’ to ‘T[]’

Any ideas of how to achieve my goal? 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-05-14T15:19:58+00:00Added an answer on May 14, 2026 at 3:19 pm

    Edit: Updated answer. The library is returning an object array, you will not be able to simply return it as T, be it an array or a single element, without doing some work with it. The function below is an example of taking an array of objects and either returning it as an array or a single element.

    public static T GetValue<T>(object[] inputs)
    {
        if (typeof(T).IsArray)
        {
            Type elementType = typeof(T).GetElementType();
            Array array = Array.CreateInstance(elementType, inputs.Length);
            inputs.CopyTo(array, 0);
            T obj = (T)(object)array;
            return obj;
        }
        else
        {
            return (T)inputs[0];
            // will throw on 0-length array, check for length == 0 and return default(T)
            // if do not want exception
        }
    }
    

    Example of consuming it:

    object[] inputs = { 1f, 2f, 3f }; // what the library is returning
    float[] array = GetValue<float[]>(inputs); // what you want?
    float singleValue = GetValue<float>(inputs); // what you want?
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm able to call a 3rd party vendor's web service from a Windows form
I'm writing a wrapper class around the V8 engine so that eventually I'll be
I am writing a .NET wrapper class for an existing native class which throws
I am currently writing an open source wrapper for a COM object. I have
I'm writing out XML files using the MSXML parser, with a wrapper I downloaded
Writing a JSP page, what exactly does the <c:out> do? I've noticed that the
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
Use case: 3rd party application wants to programatically monitor a text file being generated
I'm using SQLite to store my data. I'm writing wrapper class, and I want
I'm writing a wrapper in .NET (C++/CLI) to be able to use some native

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.