There isn't a native exception in the .NET Framework for "Invalid Type Parameter Argument." Jon Skeet asked about this on Stack Overflow, and ultimately decided to write his own:
#region License and Terms
// Unconstrained Melody
// Copyright (c) 2009-2011 Jonathan Skeet. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
using System;
using System.Runtime.Serialization;
namespace UnconstrainedMelody
{
/// <summary>
/// Exception thrown to indicate that an inappropriate type argument was used for
/// a type parameter to a generic type or method.
/// </summary>
public class TypeArgumentException : Exception
{
/// <summary>
/// Constructs a new instance of TypeArgumentException with no message.
/// </summary>
public TypeArgumentException()
{
}
/// <summary>
/// Constructs a new instance of TypeArgumentException with the given message.
/// </summary>
/// <param name="message">Message for the exception.</param>
public TypeArgumentException(string message)
: base(message)
{
}
/// <summary>
/// Constructs a new instance of TypeArgumentException with the given message and inner exception.
/// </summary>
/// <param name="message">Message for the exception.</param>
/// <param name="inner">Inner exception.</param>
public TypeArgumentException(string message, Exception inner)
: base(message, inner)
{
}
/// <summary>
/// Constructor provided for serialization purposes.
/// </summary>
/// <param name="info">Serialization information</param>
/// <param name="context">Context</param>
protected TypeArgumentException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
T
", whereT
is the type parameter. – Theodoros Chatzigiannakis Oct 16 '14 at 20:53ArgumentException
generally refers to the formal parameter list, and the name of the offending parameter is the usual argument to ArgumentException's constructor, so Resharper is basically complaining that you're referring to a parameter name that's not one of the declared method parameters (when you throw the exception). Can you use a Type Constraint instead? – Robert Harvey Oct 16 '14 at 20:55T
(specifically, public interfaces - neither of which can be expressed in the constraint). So I'd like to know if there is perhaps another kind of exception that I should be throwing in this case. (Resharper's complaint isn't my main concern - maybe I should remove that note.) – Theodoros Chatzigiannakis Oct 16 '14 at 20:58