Methods with more than one Generic

Asked

Viewed 114 times

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#

  • @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.

  • 1

    You’re not using more than one there, you’re just using TResult, wants to TType be used too? Why? Where? This restriction does not make any sense. If to do this is better not to use Generics.

  • @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.

  • 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.

  • 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?

  • Really in this case it would not fit to use the Generic :/

  • @Mustache, could you help me with that? https://answall.com/questions/212851/setar-datasource-datagridview-utiizando-heran%C3%A7a-visual-c

  • @Nicolabogar later I see.

Show 4 more comments

2 answers

2

This code will work best so:

public static TResult ToEntityForType<TResult>(this DataRow row, Type type) {
    var entity = Activator.CreateInstance<type>() as TResult;
    var properties = entity.GetType().GetProperties().ToList();
    foreach (var item in row.Table.Columns) {
        if (properties.Single(x => String.Compare(x.Name, item.ColumnName, StringComparison.OrdinalIgnoreCase) == 0) != null) {
            prop.SetValue(entity, String.IsNullOrWhiteSpace(Convert.ToString(item)) ? null : item));
    }
    return entity;
}

I put in the Github for future reference.

If you had a minimal, complete and verifiable example I would show working. I might have missed some detail.

In this case you do not need genetic Type.

There are some other details that can be made much simpler and more correct, the original code would give error in various situations. This is not in the best way possible, but it is an improvement.

Generics is a form of polymorphism. It should only be used if it is necessary, if it really can receive several different types. In case Type is only inherited by a guy throughout . NET, if you didn’t inherit from it, you don’t have to use Generics, no polymorphism. Just use the direct type.

Place object in place is not solution, is worse a situation that got wrong. object only works like this because in the beginning C# didn’t have Generics and it was the only way to generalize something. But I repeat, in this case generalization is not even necessary.

How AP seems to be obsessed with Generics I’ll make it clear:

Just use Generics where it really is necessary, where it brings benefits. I study the subject more deeply before trying to use it. The TResult is necessary. I’m just not sure if it can be solved with it in this case.

I doubt if you even need reflection, then I would need more context.

  • @bigwon, The problem ai of using the Tresult, when I called the method I will need to call it so : Toentityfortype<Parents>(Type), but I can not fail in my code that <Parents> understand? so I didn’t use the Generic, otherwise I have no way to specify the typo of this Generic otherwise.

  • There’s nothing like that in the question, so it’s unclear. There’s probably a better way to do this. I can’t even help anymore because there’s no context.

  • we ended up arriving in the context of this question that I edited today at your request. https://answall.com/questions/212901/passagem-de-par%C3%A2metro-Generics-em-c

1

I solved my question by returning an Object and using Type as parameter to be able to create the instance of my object.

    public static object ToEntityForType(this DataRow row, Type type)
    {
        EntityBase entity = Activator.CreateInstance(type) as EntityBase;

        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;
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.