score:2
Because you have the constraint that the type you are converting to implements the IConvertible
interface (whereas the Guid
structure) does not, you have no choice to create an overload, like this:
public static Guid ToType(this object val, Guid alt)
{
try
{
// Convert here.
}
catch
{
return alt;
}
}
When you pass a Guid
, it will resolve because of section 7.4.2 of the C# specification (emphasis mine):
Once the candidate function members and the argument list have been identified, the selection of the best function member is the same in all cases:
- Given the set of applicable candidate function members, the best function member in that set is located.
Given that Guid
is a more specific match than the type parameter T
, your second method will be called.
Note, if you removed the IConvertible
interface constraint, you could handle this in a single method, but you'd have to be able to have logic to handle any structure that is passed for T
(a TypeConverter
instance would be handy here).
Source: stackoverflow.com
Related Articles
- Extension method : how to make it work with GUID also
- Is there any way to make Code Contracts work with LINQ?
- Get Non-Static MethodInfo for IEnumerable<T>.First() (Or make the static method work with EF)
- Converting LINQ Extension method to work with L2E
- How to make an extension method for a IDictionary with a IList in a TValue?
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- When to use an extension method with lambda over LINQtoObjects to filter a collection?
- "CLR detected an Invalid Program" when using Enumerable.ToDictionary with an extension method
- Writing an extension method to help with querying many-to-many relationships
- How to: Use async methods with LINQ custom extension method
- How to call extension method "ElementAt"of List<T> with reflection?
- How does method overload resolution work (LINQ Where extension method)?
- Entity framework paging with extension method is slow?
- LINQ - Writing an extension method to get the row with maximum value for each group
- Add a LINQ or DBContext extension method to get an element if not exist then create with data in predicate (FirstOrCreate)
- IQueryable Where Extension Method with Or's
- Generic extension method with custom return type
- Convert linq query expression with multiple froms into extension method syntax
- Using LINQ SequenceEqual extension method with occassionally null properties
- Efficient method to work with JSON
- Why ExpandoObject doesn't work properly with Guid converted to string?
- Extension method with optional generic argument
- How To Use C# Linq Predicate with custom Extension Method
- Need help with this basic Contains<>() extension method and Lambda expressions
- Can a LINQ extension method create a new KeyValuePair with a new() .Value when a Where clause isn't satisfied
- How to use System.Linq extension method in VB.NET project with Option Strict ON
- LINQ extension method for multiple join with multiple GroupBy requirements
- Extension method with generic Func parameter of other type
- Does a code that combines single() with yield make any sense?
- most efficient Entity Framework Code First method of flattening / projecting parent entity with specific child
- Linq to SQL: Using int? as a parameter in a query
- Using Linq to group by multiple columns in a list and mark all duplicate items
- Using Linq to query repository returning invalid results
- Custom Matching with Moq
- Find XmlNode where attribute value is contained in string
- Accessing Elements from multi-level (nested) XML documents in c#
- Lambda expression where column is equal to list items
- Strange SQL Performance: Parameter slower than Function call
- Select rows from a table according to indexes on other list
- Unable to access instance variable with an Extension Method with Lambda
- Left Join, Group By and Sum where related records don't exist
- Hierarchical structure iteration and LINQ
- How do I find duplicates in XML if an element contains value in C#?
- System.FormatException: String was not recognized as a valid DateTime
- LinqPad Not Returning Results With C# Statements
- Linq Group result to an Object
- How to validate that 4 fields are not repeated in any manner
- Self join in LINQ
- LINQ-TO-SQL query not working with CSV file
- Joining Two list using LINQ Query and displaying only relevant data