This seems like it would be possible.
protected SameObjectTypeAsInputParameterObjectType GetAValue(someObject,TypeOrReturnObjectYouWant){
//check to see if someObject is null, if not, cast it and send the cast back
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In your given example you are probably better served just doing the following:
However to answer your question – yes, the answer is to use generics:
In this method T is a type parameter. You can use T in your code in exactly the same way that you would any other type (for example a string), however note that in this case we haven’t placed any restriction on what T is, and so objects of type T only have the properties and methods of the base
object(GetType(),ToString()etc…)We must obviously declare what T is before we can use it – for example:
For more information take a look at the MSDN documentation on Generics