There are two common approaches. First, you can pass System.Type
object GetColumnValue(string columnName, Type type) { // Here, you can check specific types, as needed: if (type == typeof(int)) { // ...
This would be called like: int val = (int)GetColumnValue(columnName, typeof(int));
The other option would be to use generics:
T GetColumnValue<T>(string columnName) { // If you need the type, you can use typeof(T)...
This has the advantage of avoiding the boxing and providing some type safety, and would be called like: int val = GetColumnValue<int>(columnName);
Related Posts:
- Passing just a type as a parameter in C#
- Why has the int32 type a maximum value of 2³¹ − 1? [duplicate]
- Visual Studio says “Method must have a return type”
- String was not recognized as a valid DateTime ” format dd/MM/yyyy”
- Verify a method call using Moq
- How to create a new object instance from a Type
- Type Checking: typeof, GetType, or is?
- How do I generate a random int number?
- An existing connection was forcibly closed by the remote host
- Unexplained crashes related to ntdll.dll
- I am getting Failed to load resource: net::ERR_BLOCKED_BY_CLIENT with Google chrome
- What is parsing?
- How to fix “HTTP Error 400. The size of the request headers is too long” error from azure cloud services?
- Could not load file or assembly or one of its dependencies
- Adding values to a C# array
- LINQ’s Distinct() on a particular property
- Priority queue in .Net
- What is the difference between int, Int16, Int32 and Int64?
- Adding values to a C# array
- How can I add ” character to a multi line string declaration in C#?
- Input string was not in a correct format
- The request was aborted: Could not create SSL/TLS secure channel
- Unable to connect to web server ‘IIS Express’
- Twilio TwilioRestClient does not contain a definition for SendSmsMessage
- How to convert byte array to string
- ASP.NET Core Dependency Injection error: Unable to resolve service for type while attempting to activate
- System.web.mvc missing
- Nullable object must have a value?
- Sequence contains no elements?
- Compiler Error Message: The compiler failed with error code -2146232576
- Best way to generate a random float in C#
- Check if list is empty in C#
- What is clr.dll on .Net framework and what does it do?
- How can I secure passwords stored inside web.config?
- How to Use TextureBrush for painting an Image
- How to Use TextureBrush for painting an Image
- C# compiler error: “not all code paths return a value”
- Can’t use System.Windows.Forms
- How do I read and parse an XML file in C#?
- How to fix “namespace x already contains a definition for x” error? Happened after converting to VS2010
- How do I create a Shared Code project (.shproj)
- OnCollisionEnter2D not being executed?
- The calling thread cannot access this object because a different thread owns it
- Convert JSON String To C# Object
- How can I fix Visual Studio 2015 exception Microsoft.vshup.server.httphostx64.exe has stopped working when run project
- IEnumerable vs List – What to Use? How do they work?
- Merge two (or more) lists into one, in C# .NET
- Why should I use IHttpActionResult instead of HttpResponseMessage?
- Convert integer to binary in C#
- HTML.ActionLink method
- Thread-safe List
property - Styles.Render in MVC4
- Exception from HRESULT: 0x800A03EC Error
- num1 >= 0x80 – What is 0x80?
- Sharing violation IOException while reading and writing to file C#
- How to delete object?
- Carriage return and Line feed… Are both required in C#?
- Inconsistent Accessibility: Parameter type is less accessible than method
- How to get correct timestamp in C#
- There is already an open DataReader associated with this Command which must be closed first
- Merging two arrays in .NET
- C# syntax to initialize custom class/objects through constructor params in array?
- Unable to connect to any of the specified mysql hosts. C# MySQL
- Checking if a string array contains a value, and if so, getting its position
- Substring index and length must refer to a location within the string
- Difference between Method and Function?
- How to pass data (and references) between scenes in Unity
- Constructor of an abstract class in C#
- How to force garbage collector to run?
- Page vs Window in WPF?
- CS1617: Invalid option ‘6’ for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default
- Catch multiple exceptions at once?
- How to delete a file after checking whether it exists
- c# Show Windows Form
- Equivalent of console.log in C#
- How to get the URL of the current page in C#
- C# Debug – cannot start debugging because the debug target is missing
- ‘namespace’ but is used like a ‘type’
- How to properly make a http web GET request
- Strip double quotes from a string in .NET
- Convert double to float by cast or Convert.ToSingle()?
- Simulating Key Press C#
- Call a stored procedure with parameter in c#
- No overload for method ” takes 1 argument
- Best way to “push” into C# array
- C# Macro definitions in Preprocessor
- Python-like list comprehension in Java
- Is it possible to install a C# compiler without Visual Studio?
- Playing a MP3 file in a WinForm application
- The server tag is not well formed error
- How to solve Operator ‘!=’ cannot be applied to operands of type ‘T’ and ‘T’
- Best way to randomize an array with .NET
- Encrypt and decrypt a string in C#?
- Key Value Pair List
- Using .Select and .Where in a single LINQ statement
- How to resize an Image C#
- Unrecognized escape sequence for path string containing backslashes
- C# Dictionary get item by index
- the source file is different from when the module was built
- A field initializer cannot reference the nonstatic field, method, or property