C # Is it possible to pass a type to a method and return the method to this type?

It seems that would be possible.

protected SameObjectTypeAsInputParameterObjectType GetAValue(someObject,TypeOrReturnObjectYouWant){ //check to see if someObject is null, if not, cast it and send the cast back } 
+7
c #
source share
7 answers

In your example, you are probably best using the following:

 MyClass val = myObject as MyClass; 

However, to answer your question - yes, the answer is to use generics:

 protected T GetAValue<T>(object someObject) { if (someObject is T) { return (T)someObject; } else { // We cannot return null as T may not be nullable // see http://stackoverflow.com/questions/302096/how-can-i-return-null-from-a-generic-method-in-c return default(T); } } 

In this method, T is a type parameter. You can use T in your code just like any other type (for example, a string), however, note that in this case we do not restrict T, and therefore objects of type T have properties and methods of the object ( GetType() base , ToString() , etc.)

We must obviously declare what T is before we can use it - for example:

 MyClass val = GetAValue<MyClass>(myObject); string strVal = GetAValue<string>(someObject); 

See the MSDN documentation for shared files for more information.

+8
source share

It seems that this would be done better inline.

What you can do is something like:

 var requestedObject = someObject as TheTypeYouWant; 

When you declare something like this, you will not get a null-reference exception. Then you can just make simple

 if(requestedObject != null){ ... } 
+5
source share

Use generics:

 T foobar<T>() where T : new() { return new T(); } void somefunc() { Guid g = foobar<Guid>(); } 
+3
source share

You can define a method as follows:

 protected TReturnType GetAValue<TReturnType>(Object someObject) where TReturnType : class { if(someObject is TReturnType) return (TReturnType) someObject; return null; } 

Alternatively, if you cannot guarantee that TReturnType is a reference type, you can waive the general restriction (TReturnType: class) and return the default value (TReturnType) instead of null - this will return the default TReturnType type, which will be 0 for int. ..

+1
source share

If you know the type at compile time (when writing code), you can use generics.

If you only know the type at runtime (via type), you can use the Activator class.

+1
source share

Such a conversion can be done with as

 requestedObject as RequestedType 

The wrapper of this expression in the method really doesn’t buy anything

+1
source share

I think you can really find the Convert.ChangeType mechanism.

 private static object ChangeTypeTo<T>(this object value) { if (value == null) return null; Type underlyingType = typeof (T); if (underlyingType == null) throw new ArgumentNullException("value"); if (underlyingType.IsGenericType && underlyingType.GetGenericTypeDefinition().Equals(typeof (Nullable<>))) { var converter = new NullableConverter(underlyingType); underlyingType = converter.UnderlyingType; } // Guid convert if (underlyingType == typeof (Guid)) { return new Guid(value.ToString()); } // Check for straight conversion or value.ToString conversion var objType = value.GetType(); // If this is false, lets hope value.ToString can convert otherwise exception bool objTypeAssignable2typeT = underlyingType.IsAssignableFrom(objType); // Do conversion return objTypeAssignable2typeT ? Convert.ChangeType(value, underlyingType) : Convert.ChangeType(value.ToString(), underlyingType); } 
+1
source share

All Articles