Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 474 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generic TryParse

#1
I am trying to create a generic extension that uses 'TryParse' to check if a string is a given type:

public static bool Is<T>(this string input)
{
T notUsed;
return T.TryParse(input, out notUsed);
}

this won't compile as it cannot resolve symbol 'TryParse'

As I understand, 'TryParse' is not part of any interface.

Is this possible to do at all?

**Update:**

Using the answers below I have come up with:

public static bool Is<T>(this string input)
{
try
{
TypeDescriptor.GetConverter(typeof(T)).ConvertFromString(input);
}
catch
{
return false;
}

return true;
}

It works quite well but I think using exceptions in that way doesn't feel right to me.

**Update2:**

Modified to pass type rather than use generics:

public static bool Is(this string input, Type targetType)
{
try
{
TypeDescriptor.GetConverter(targetType).ConvertFromString(input);
return true;
}
catch
{
return false;
}
}
Reply

#2
This is a question of 'generic constraints'. Because you don't have a specific interface then you are stuck unless you follow the suggestions of the previous answer.

For documentation on this, check the following link:

[To see links please register here]


It shows you how to use these constraints and should give you some more clues.
Reply

#3
You can't do it on general types.

What you could do is to create an interface ITryParsable and use it for custom types that implement this interface.

I guess though that you intend to use this with basic types like `int` and `DateTime`. You can't change these types to implement new interfaces.
Reply

#4
As you said, `TryParse` is not part of an interface. It is also not a member of any given base class since it's actually `static` and `static` functions can't be `virtual`. So, the compiler has no way of assuring that `T` actually has a member called `TryParse`, so this doesn't work.

As @Mark said, you could create your own interface and use custom types, but you're out of luck for the built-in types.
Reply

#5
When I wanted to do almost this exact thing, I had to implement it the hard way, given reflection. Given `T`, reflect on `typeof(T)` and look for a `TryParse` or `Parse` method, invoking it if you've found it.
Reply

#6
If you are set on using TryParse, you can use reflection and do it like this:

public static bool Is<T>(this string input)
{
var type = typeof (T);
var temp = default(T);
var method = type.GetMethod(
"TryParse",
new[]
{
typeof (string),
Type.GetType(string.Format("{0}&", type.FullName))
});
return (bool) method.Invoke(null, new object[] {input, temp});
}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through