Pass Type As Parameter
In short, add <T> after the function name, pass in a parameter with type T.Type, and when you need to use that parameter, usually you call the name of the parameter, but instead, you use the type T
Pass in Type as Parameter
Why do I need to pass in types as a parameter? One of the usage case is I want to dynamically cast some objects depending to what I passed in.
How?
Here is a useful reference I found.
In short, add <T> after the function name, pass in a parameter with type T.Type, and when you need to use that parameter, usually you call the name of the parameter, but instead, you use the type T
Call class method when pass in custom object
Here is a useful reference I found.
In short, create a protocol with the method you want to call and adapt to the protocol in that custom object, then when you create a function that pass in the type T, assign the protocol to that type, then its good to go.
1 | protocol MethodProtocol { |
