Not really; C# only supports no-args constructor constraints.
The workaround I use for generic arg constructors is to specify the constructor as a delegate:
public T MyGenericMethod<T>(MyClass c, Func<MyClass, T> ctor) {
// ...
T newTObj = ctor(c);
// ...
}
then when calling:
MyClass c = new MyClass();
MyGenericMethod<OtherClass>(c, co => new OtherClass(co));
Related Contents:
- How to define generic type limit to primitive types?
- Pass An Instantiated System.Type as a Type Parameter for a Generic Class
- How to get the type of T from a member of a generic class or method
- In C#, why can’t a List object be stored in a List variable
- Passing arguments to C# generic new() of templated type
- What exactly is an “open generic type” in .NET? [duplicate]
- Generics in C#, using type of a variable as parameter [duplicate]
- Why isn’t there generic variance for classes in C# 4.0?
- Create instance of generic type whose constructor requires a parameter?
- Testing if object is of generic type in C#
- What’s the difference between SortedList and SortedDictionary?
- No generic implementation of OrderedDictionary?
- Why must I provide explicitly generic parameter types While the compiler should infer the type?
- C# Create New T()
- Does .NET have a way to check if List a contains all items in List b?
- Func with out parameter
- How do I implement IEnumerable
- foreach vs someList.ForEach(){}
- Why cannot C# generics derive from one of the generic type parameters like they can in C++ templates? [duplicate]
- What does “where T : class, new()” mean?
- No type inference with generic extension method
- Passing a single item as IEnumerable
- How do I make the return type of a method generic?
- How to compare values of generic types?
- How to call generic method with a given Type object? [duplicate]
- Why isn’t ArrayList marked [Obsolete]?
- Why can’t I assign a List to a List?
- What does “T” mean in C#?
- How to initialize a List to a given size (as opposed to capacity)?
- Get a generic method without using GetMethods
- Value of type ‘T’ cannot be converted to
- Static Generic Class as Dictionary
- Convert string to nullable type (int, double, etc…)
- explicitly cast generic type parameters to any interface
- Anonymous Types – Are there any distingushing characteristics?
- Generic Type in constructor
- A reusable pattern to convert event into task
- Convert DataTable to generic List?
- C# generic “where constraint” with “any generic type” definition?
- Generics: casting and value types, why is this illegal?
- Generic List – moving an item within the list
- Generics can’t infer second parameter? [duplicate]
- Ambiguous call between two C# extension generic methods one where T:class and other where T:struct
- Parse string to enum type
- Why generic IList does not inherit non-generic IList
- List readonly with a private set
- Declare a generic type instance dynamically [duplicate]
- Returning ‘IList’ vs ‘ICollection’ vs ‘Collection’
- Mocking generic methods in Moq without specifying T
- Why can’t I catch a generic exception in C#?