简体   繁体   English

C#-在运行时确定属性类型

[英]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. 乍看之下,如果T是使用此类实例创建的泛型类型,则似乎很简单且可行。 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. 但是我要做的是根据Image属性中的确定结果提供字符串或位图,这样就可以将Image作为Image服务器的知识包含在Image属性中,而其他任何地方都不需要知道。 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. 然后,您将可以使用通用类型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. 由于仅返回都是引用类型的StringBitmap ,因此可以使用object,而不必担心装箱或拆箱。

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. 例如,为将要成为WebContext的所有内容创建一个单独的类,并实现一个公共接口。

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. 现在,假设您正在使用returntype对象。 Then you can verify if the object instance that is returned is of a certain type using the is operator. 然后,您可以使用is运算符验证返回的对象实例是否为某种类型。

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. 其次,我不建议在每个属性中检查IsWebContext 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());    
       }
   } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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