I have something like this
public void FunctionName<T>(){
}
and to call this you need something like this
sub main(){
FunctionName<Integer>();
}
my question is can i pass the Integer as “Integer”?
sub main(){
FunctionName<"Integer">();
}
or is there any other way to that
Ok here is my actual code
private static T Convert<T>(DataRow row) where T : new()
{
T model = new T();
Type modelType = model.GetType();
FieldInfo[] fields = null;
FieldInfo field = default(FieldInfo);
Type fieldType = null;
string fieldName = null;
fields = modelType.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);
foreach ( field in fields) {
fieldType = field.FieldType;
fieldName = field.Name.Replace("_", string.Empty);
Interaction.CallByName(model, fieldName, Microsoft.VisualBasic.CallType.Set, GetValue(row, fieldName, fieldType.Name));
}
return model;
}
static void main(){
Student s;
s = Convert<Student>(DatabaseRow);
}
The problem here is I just can only get the Student as
string (“Student”) from somewhere that will use this code
Any other solution to get this right?
Without seeing more of your code, it’s hard to say for sure whether you have a legitimate use-case for invoking a method like this. But regardless, assuming
FunctionNameis contained in classFoo, then you can invoke the method using reflection:Note that we can’t use
Integersince that’s not the real type name. (it’s eitherintorInt32in C# but onlyInt32is legal forGetTypesinceintis baked into the compiler) Also, all the usual caveats about getting a type viaType.GetTypeapply here (there are many situations where it won’t find your custom type).