Is it possible to implement method to call like
if (myString is Email)
where Email
is a class name and actual validation is performed under "is", not just type checking.
Not for real world projects, just curious.
Is it possible to implement method to call like
if (myString is Email)
where Email
is a class name and actual validation is performed under "is", not just type checking.
Not for real world projects, just curious.
No you can't because is
operator cannot be overloaded. Read here
Why don't you create a custom extension method and check in that method.
You may find useful a partial solution via operator:
public class Email {
...
public static implicit operator Email(String value) {
if (...) // validate value here
return null; // <- not an actual Email
// Email
return new Email(value);
}
}
....
Email email = "SomeAddress@SomeServer.com";
if (email != null) {
...
}