简体   繁体   中英

C# - Determine property type at runtime

Here is what I want to do:

public [type determined at runtime] ImageToShow
{
  get
  {
    if(this.IsWebContext)
    {
       return this.GetString();
    }
    else
    {
       return this.GetBitmap();
    } 
  }
}

At first look it seems simple and doable if T was the generic type which with instance of this class was created. But what I want to do is serve a String or Bitmap based on determination made within the Image property so that the knowledge of what to server as Image is contained within the Image property and no place else needs to know about it. I can certainly make the return type 'object' and it will work, but I don't want boxing and unboxing inefficiency neither do I want reflection involved.

I just wanted to check with you guys if this is possible before I give up on this idea.

Would it not be better for the caller to "know", using a public property

YourClass.IsWebContext

what to expect?

Then you would be able to use the generic type T.

Boxing occurs when you convert a value type to a reference type.

int i = 5;

object o = i; // Boxing

Since you are returning only String or a Bitmap , which are both reference types, you can use object without having to worry about boxing or unboxing.

Seems like instead of working around this you should consider a different design. For instance creating a separate class all together for everything that is going to be WebContext and implementing a common interface.

First of all, returning reference types as an object isn't boxing. Boxing only occurs when using a valuetype as a referencetype.

Now let's say you are using the returntype object. Then you can verify if the object instance that is returned is of a certain type using the is operator.

object o = myClass.ImageToShow;

if (o is String)
{
  // Use as a String
}
else if (o is Bitmap)
{
  // Use as a Bitmap
}

Secondly, I wouldn't recommend checking IsWebContext in every property. It would make more sense to create a base class, and specialize given the environment it is used in.

Yes, use an interface.

   public interface IAmImage {}
   public class StringImage: IAmImage
   {
      private string img;
      public string Image { get { return img; } set { img = value; } }
      public StringImage(string image) { img = image;}
   }
   public class BitmapImage: IAmImage
   {
      private Bitmap img;
      public Bitmap Image { get { return img; } set { img = value; } }
      public BitmapImage(Bitmap image) { img = image;}
   }

... and in your client code ....

   public IAmImage ImageToShow 
   {  
       get  
       {    
           return this.IsWebContext?
              new StringImage(this.GetString()):        
              new BitmapImage(this.GetBitmap());    
       }
   } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM