Is there a better way to instantiate a generic type from an actual type variable (with a parameter in the constructor)? In the following code, I abstracted the call to use a helper method. This works, however, resharper complains that the type is an unused variable.
public class CustomSerializerOperationBehavior : DataContractSerializerOperationBehavior
{
public CustomSerializerOperationBehavior (OperationDescription operation) : base(operation) { }
public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes)
{
return Helper(type, base.CreateSerializer(type, name, ns, knownTypes));
}
public override XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, IList<Type> knownTypes)
{
return Helper(type, base.CreateSerializer(type, name, ns, knownTypes));
}
// Note update gets the correct type, but I would like to call a specific construct
private XmlObjectSerializer Helper(Type type, XmlObjectSerializer serializer)
{
var genericListType = typeof(JilSerializer<>);
var specificListType = genericListType.MakeGenericType(type);
return Activator.CreateInstance(specificListType, serializer) as XmlObjectSerializer;
}
}