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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:52:11+00:00 2026-05-31T11:52:11+00:00

Suppose I have the following class: public class MyClass { public decimal myDecimal; public

  • 0

Suppose I have the following class:

public class MyClass
{
    public decimal myDecimal;
    public string myString;
}

I want to use the DataRowExtensions method Field<>

Currently, I am using the class like so:

MyClass myClass = new MyClass();
myClass.myDecimal = row.Field<decimal>("MyDecimalColumnName");
myClass.myString = row.Field<string>("MyStringColumnName");

However, if I ever decide to change the type of myDecimal to something other than decimal, I want the call to row.Field to reflect the correct data.

I want something similar to the following syntax:

myClass.myDecimal = row.Field<typeof(myClass.myDecimal)>("MyDecimalColumnName");

This doesn’t compile, and I have no idea how to use typeof or GetType() to just return decimal, whatever that would be called.

Is there a way to do this, or something similar? I figured this could accomplished at compile time as the types are already known, and since generics are compile time constructs.

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-31T11:52:13+00:00Added an answer on May 31, 2026 at 11:52 am

    First, note that public fields are usually a very bad idea; but if we assume this was a private backing field, there are 2 interesting options here;

    The first is to exploit generic type inference; this doesn’t work for the return type, but does for the parameters, so you could have:

    row.GetField("MyDecimalColumnName", out obj.someField);
    

    where that is:

    GetField<T>(string name, out T value);
    

    Another trick would be to use implicit operators, i.e. have GetField(string) return a dummy object that has implicit conversion operators to a few types such as int, decimal, etc, and do the work / conversion in the operator. A bit hacky, but would work – syntax would be:

    myClass.myDecimal = row.Field("SomeColumn")
    

    with:

    SomeDummyType GetField(string name);
    

    and with SomeDummyType having an implicit static conversion operator or several.

    However! IMO the best option here is to use a tool such as an ORM or micro-ORM to load the values for you, and don’t use DataRow at all.

    Another simple option is just to go old-school:

    myClass.myDecimal = (decimal)row["SomeColumn"];
    

    I mean – is the .Field<T> really helping you all that much? Do you genuinely refactor your classes often enough that this is worth worrying about it?

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

Sidebar

Related Questions

Suppose I have the following: public class MyObject { public string Name {get; set;}
Suppose I have the following class: template <typename T> class MyClass { public: void
Suppose I have the following class: public class Test{ public string Length { get;
Suppose I have the following classes: public class Test { public static void main(String[]
Suppose I have the following class: class Camera { public Camera( double exposure, double
Suppose you have the following class: class Test : ISerializable { public static Test
Suppose I have the following (trivially simple) base class: public class Simple { public
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Suppose I have the following 2 entities: @Entity public class Person implements Serializable {
Suppose I have the following service object public class UserService { @Autowired private UserDao

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.