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);
Let's have a look at and classType:T
public static string ComposeHtmlTable<T>(Type classType, IList<T> table)
{
List<classType> test = table.Cast<classType>().ToList();
Console.WriteLine(test[0].CaseID[0]); // trying to access data
return "test";
}
Can they be some arbitrary types, say and T == int?
Definitely not. As I can see, both classType == StringBuider and classType should be inherited from T.
Let's .net know it:TableOne
public static string ComposeHtmlTable<C, T>(IList<T> table)
where C : TableOne
where T : TableOne
{...}
Time to add some details:
IList<T> table as an argument, IEnumerable<T> table is enough (and we will be able to pass not only lists, by, say, arrays)public, any input is possible; so we have to validate input argumentstest is empty (i.e. it doesn't have any items?). We can't obtain test[0].CaseID[0] in this case.public static string ComposeHtmlTable<C, T>(IEnumerable<T> table)
where C : TableOne
where T : TableOne
{
if (table is null)
throw new ArgumentNullException(nameof(table));
List<C> test = table.Cast<C>().ToList();
if (test.Count > 0 && test[0] != null && test[0].CaseID.Count > 0)
Console.WriteLine(test[0].CaseID[0]); // trying to access data
else
Console.WriteLine("test is empty");
return "test";
}
The keyword when you know the class at compile time.typeof
SetValuesToObject(typeof(Product),datarow);
You can also use on instances that you don't know their type at compile time.object.GetType()
Sounds like you need a generic method
public void SetException<TException>() where TException : Exception
{
ExceptionCounts[typeof(TException)]++;
}
You can call it as
SetException<InvalidProgramException>();
Edit:
Dictionary<Type, int> ExceptionCounts;
public void ExceptionSeen(Type type)
{
ExceptionCounts[type]++;
}
Call it as
ExceptionSeen(typeof(MyException));
Or if you have the exception instance already
ExceptionSeen(ex.GetType());
I'm not exactly sure what you are trying to do, but you can generalize methods as .Delegate
static void Main(string[] args)
{
MyMethod(new Action(Foo.MethodA));
MyMethod(new Func<int,int>(Foo.MethodB));
}
public static void MyMethod(Delegate del)
{
// Example code
var methodInfo = del.Method;
var methodName = methodInfo.Name;
}
You will still need to wrap the method in some delegate like or ActionFunc<>
A class is just a type. You pass a type by declaring your method like this
void writeSettings(Type[] types)
{
and calling it like this:
writeSettings(new Type[] { typeof(Settings), typeof(FileSettings) });
You can also use params for a more convenient syntax:
void writeSettings(params Type[] types)
{
writeSettings(typeof(Settings), typeof(FileSettings) ); //Don't need to build an array
Anyway I wrote a little code that gets the properties for you into a dictionary:
public static Dictionary<FieldInfo,object> ReadStaticFields(params Type[] types)
{
return types
.SelectMany
(
t => t.GetFields(BindingFlags.Public | BindingFlags.Static)
)
.ToDictionary(f => f, f => f.GetValue(null) );
}
And you can use it like this:
var settings = ReadStaticFields
(
typeof(Globalsettings.Settings),
typeof(Globalsettings.FileSettings)
);
foreach (var s in settings)
{
Console.WriteLine
(
"{0}.{1} = {2}({3})",
s.Key.DeclaringType,
s.Key.Name,
s.Value,
s.Key.FieldType.Name
);
}