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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:43:03+00:00 2026-05-23T09:43:03+00:00

I need to pass value from array to method that has where T :

  • 0

I need to pass value from array to method that has where T : struct generic constraint.

I have object[] values; That for example contains { (int)7, (byte)3, (char)'a' } (i don’t know structure at compile time)

and i need to pass that array, or all elements one by one to any of following methods

DataStream(Array array)
DataStream(IntPtr ptrToArray, int sizeInbytes)

DataStream.Write<T>(T value) where T : struct
DataStream.WriteBytes(byte[] bytes)

First method will write some error in GCHandle.Alloc (that object does not contain primitive data)
Third mthod will not compile since object is not struct

Actual problem is that i am trying to create MeshBuilder in SlimDX that will contain VertexChannels and then build VertexBuffer out of them

  • 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-23T09:43:04+00:00Added an answer on May 23, 2026 at 9:43 am

    Well, your array will contain boxed values which is why the first two will fail. You’re not going to be able to do this all in one call.

    Options:

    • Call the generic Write method using reflection, picking up the execution-time type of the values that way:

      // You may well need to do more work here, if DataStream.Write is
      // overloaded.
      MethodInfo genericMethod = typeof(DataStream).GetMethod("Write");
      foreach (object value in values)
      {
          Type type = value.GetType();
          MethodInfo method = genericMethod.MakeGenericMethod(type);
          method.Invoke(dataStream, value);
      }
      
    • Use dynamic typing if you’re using C# 4 and .NET 4:

      foreach (dynamic value in values)
      {
          dataStream.Write(value);
      }
      
    • Explicitly check each value against your supported types:

      foreach (object value in values)
      {
          if (value is char)
          { 
              dataStream.Write((char) value);
          }
          else if (value is byte)
          {
              dataStream.Write((byte) value);
          }
          else if (value is int)
          {
              dataStream.Write((int) value);
          }
          else
          {
              throw new ArgumentException("Unsupported type in input data");
          }
      }
      

    All of these are likely to be fairly slow, to be honest. The first is likely to be the slowest, but I wouldn’t like to bet on anything without testing it first.

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

Sidebar

Related Questions

I need to pass an array from JavaScript to a page method in C#.
I am creating a function that I need to pass in values from an
I need to pass an array of numeric values from jQuery to a php
I have an ASP control custom built and I need to pass a value
I have a dict, which I need to pass key/values as keyword arguments.. For
I need to create a valid xml from a given array(); My Method looks
I want to receive an array that contains all the h1 tag values from
I have list of DateTime values, and for each value I need to fetch
I have a method in C++ that takes an array of doubles as an
I need to pass a pointer through a scripting language which just has a

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.