3
It is possible to create a method using more than one type of Generic, sort of like this.
public static TResult ToEntityForType<TResult>(this DataRow row, TType type) where TType : Type
{
TResult entity = Activator.CreateInstance<TType>() as TResult;
List<PropertyInfo> properties = entity.GetType().GetProperties().ToList();
DataColumnCollection columnsTable = row.Table.Columns;
for (int i = 0; i < columnsTable.Count; i++)
{
PropertyInfo prop = properties.Single(x => x.Name.ToUpper() == columnsTable[i].ColumnName.ToUpper());
if (prop != null)
{
string result = Convert.ToString(row[i]);
if (result.Trim() == String.Empty)
prop.SetValue(entity, null);
else
prop.SetValue(entity, row[i]);
}
}
return entity;
}
Possible duplicate of Passing Generics parameter in C#
– user28595
@diegofm , one subject has nothing to do with the other, this I’m just asking if there is the possibility to create methods with more than one type of Generic, the other I ask if there is how to pass Generic parameter without actually having to pass directly the name of the Object.
– Nicola Bogar
You’re not using more than one there, you’re just using
TResult
, wants toTType
be used too? Why? Where? This restriction does not make any sense. If to do this is better not to use Generics.– Maniero
@bigown , I edited the code I tried to accomplish here, for this reason I would need two Generics, would that be possible? is that my routine is totally Generic, and if I put here from the beginning what I am doing will be a lot of things and almost impossible to explain in text.
– Nicola Bogar
You’ve created some kind of inherited from
Type
? If you didn’t create just need one. You’re trying to use Generics where it doesn’t fit. This mechanism isn’t this whole silver bullet that you think it is, and most of the things you’re trying to do are simpler without it. Some things you can only do without him.– Maniero
I managed to realize what needs @bigown, I used the return as Object, I did not want to use due to Boxing and umboxing, but it was not good. How do I put the code to say how I solved it?
– Nicola Bogar
Really in this case it would not fit to use the Generic :/
– Nicola Bogar
@Mustache, could you help me with that? https://answall.com/questions/212851/setar-datasource-datagridview-utiizando-heran%C3%A7a-visual-c
– Nicola Bogar
@Nicolabogar later I see.
– Maniero