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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:52:02+00:00 2026-06-15T18:52:02+00:00

I have the following simple function: private static Nullable<T> CastValue<T>(object val) where T :

  • 0

I have the following simple function:

private static Nullable<T> CastValue<T>(object val)
 where T : struct
{
    if (!(val is DBNull))
    {
        return (T) val;
    }
    return null;
}

I would like to call it while iterating over rows/columns of a data table like this:

var table = CreateTable();
foreach (DataRow row in table.Rows)
{
    foreach (DataColumn column in table.Columns)
    {
        Type type = column.DataType;
        CastValue<type>(row[column]);
    }
}

However, I am getting the following error:

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

Is there a way to call a generic function with a generic parameter that is determined at run time?

  • 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-15T18:52:04+00:00Added an answer on June 15, 2026 at 6:52 pm

    You can’t1 because generic arguments are resolved at compile-time, while the column’s type will only be known at run-time.

    Since you’re not doing anything with the result of CastValue it’s unclear what you’re trying to accomplish, but a cast should be unnecessary since row[column] should already be an instance of the column’s data type.


    1You can with reflection but I don’t see how it helps your situation.

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

Sidebar

Related Questions

I have the following simple function: private void EnableDisable941ScheduleBButton() { if (this._uosDepositorFrequency.Value != null)
I have a simple function as the following: static Task<A> Peirce<A, B>(Func<Func<A, Task<B>>, Task<A>>
I have the following javascript function person() { //private Variable var fName = null;
I have the following function //simple function with parameters and variable function thirdfunction(a,b,c,d){ console.log(the
I have got the following very simple code: function init() { var articleTabs =
I have the following simple jquery snippet $(document).ready(function () { $.ajax({ url:myjson.json, dataType: 'json',
Let's say I have the following, very simple block of code: <script> function hasVal(field)
I have a very simple html page with the following javascript in it $(function()
I have following simple program: import std.stdio; int main(string[] argv) { writeln(Hello, world!); return
I have following code: private void fileSystemWatcher_Changed(object sender, System.IO.FileSystemEventArgs e) { System.Diagnostics.Process execute =

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.