Generic type within Action?

A Bittersweet Life

Action construct is generic but can that support generic type inside it?

The code snippet shown below is what I'm trying to achieve.

I'm aware of that there are other ways to do it but I am curious whether it can be achieved within an Action construct.

void SomeMethod()
{
    Action<int> Initialize = //<T> and where T is all that stuff
    (index) =>
    {
        T obj = new T();
        obj.Initialize(index);
        obj.DoWork();
    };

    Initialize<TypeA>(1);
    Initialize<TypeB>(2);
}
Marc Gravell

No, basically - or at least, not within a single instance of an Action - a single instance is inherently closed in terms of generics. Obviously you could use a regular generic method Initialize<T>. If you need a single Action, then it might need to take the type as a parameter:

Action<Type, int> init = (type, index) => {
    ISomeInterface obj = (ISomeInterface)Activator.CreateInstance(type);
    obj.Initialize();
    obj.DoWork();
};
init(typeof(TypeA), 1);
init(typeof(TypeB), 2);

Otherwise:

void Initialize<T>(int index) where T : new(), ISomeInterface {
    T obj = new T();
    obj.Initialize();
    obj.DoWork();
}
void SomeMethod() {
    Initialize<TypeA>(1);
    Initialize<TypeB>(2);
}

You can create Action<int> delegates to represent separately Initialize<TypeA>(int) and Initialize<TypeB>(int), but not both at the same time, and not the open Initialize<T>(int).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Generic type within Action?

From Dev

Interface with generic type used as generic type within second interface

From Dev

Interface with generic type used as generic type within second interface

From Dev

Understanding Java bound generic type within collection

From Dev

Change generic type from within the class

From Dev

Understanding Java bound generic type within collection

From Dev

Action as generic type in constructor gives compile error

From Dev

Generic Binary Search Tree Java type argument is not within bounds

From Dev

Generic within a generic in Swift

From Dev

Use derived class type as parameter of generic action of base class

From Dev

How to create an Action<T> with Reflection where T is a discovered generic Type

From Dev

Play (Scala) handling different Content-Type within the same action

From Dev

Reading concrete class property within a generic method that doesn't get generic type instance

From Dev

How to store Action delegates with constrained generic type in a type safe collection in C#?

From Dev

How to store Action delegates with constrained generic type in a type safe collection in C#?

From Dev

java "generic generic type"

From Dev

Generic type in generic constraint

From Dev

generic as a generic type in Java?

From Dev

Generic type in generic constraint

From Dev

This generic type

From Dev

Generic Type of a Generic Type in Java?

From Dev

How can I read a type name from a DocumentDB document model from within a generic DocumentDBRepository?

From Dev

Just passing Java generic type through, but still getting not-within-its-bound error

From Dev

Casting bounded wildcard to unbounded wildcard within a generic type is an error (X<Y<? extends T>> to X<Y<?>>

From Dev

How to check that MethodInfo matches Delegate of generic type T, where T is either Action or Func?

From Dev

ASP.NET MVC Cast viewmodel into generic interface type in action filter

From Dev

Delay an action within an ActionListener?

From Dev

Using Html.Action and PartialView but get a Duplicate type name within an assembly

From Dev

Set the type of registered Service during runtime within an action filter/message handler

Related Related

  1. 1

    Generic type within Action?

  2. 2

    Interface with generic type used as generic type within second interface

  3. 3

    Interface with generic type used as generic type within second interface

  4. 4

    Understanding Java bound generic type within collection

  5. 5

    Change generic type from within the class

  6. 6

    Understanding Java bound generic type within collection

  7. 7

    Action as generic type in constructor gives compile error

  8. 8

    Generic Binary Search Tree Java type argument is not within bounds

  9. 9

    Generic within a generic in Swift

  10. 10

    Use derived class type as parameter of generic action of base class

  11. 11

    How to create an Action<T> with Reflection where T is a discovered generic Type

  12. 12

    Play (Scala) handling different Content-Type within the same action

  13. 13

    Reading concrete class property within a generic method that doesn't get generic type instance

  14. 14

    How to store Action delegates with constrained generic type in a type safe collection in C#?

  15. 15

    How to store Action delegates with constrained generic type in a type safe collection in C#?

  16. 16

    java "generic generic type"

  17. 17

    Generic type in generic constraint

  18. 18

    generic as a generic type in Java?

  19. 19

    Generic type in generic constraint

  20. 20

    This generic type

  21. 21

    Generic Type of a Generic Type in Java?

  22. 22

    How can I read a type name from a DocumentDB document model from within a generic DocumentDBRepository?

  23. 23

    Just passing Java generic type through, but still getting not-within-its-bound error

  24. 24

    Casting bounded wildcard to unbounded wildcard within a generic type is an error (X<Y<? extends T>> to X<Y<?>>

  25. 25

    How to check that MethodInfo matches Delegate of generic type T, where T is either Action or Func?

  26. 26

    ASP.NET MVC Cast viewmodel into generic interface type in action filter

  27. 27

    Delay an action within an ActionListener?

  28. 28

    Using Html.Action and PartialView but get a Duplicate type name within an assembly

  29. 29

    Set the type of registered Service during runtime within an action filter/message handler

HotTag

Archive