开发者

Converting Method to Generic Method?

I have created a Method shown below,

public BOEod CheckCommandStatus(BOEod pBo, IList<string> pProperties)
{
    pBo.isValid = false;
    if (pProperties != null)
    {
        int Num=-1;
        pBo.GetType().GetProperty(pProperties[0].ToString()).GetValue(pBo, null);
        if (ifIntegerGetValue(pBo.GetType().GetProperty(pProperties[0].ToString()).GetValue(pBo, null).ToString(), out Num))
        {
            if (Num == 1)
                pBo.isValid = true;
        }

 开发者_Go百科   }
    return pBo;
}

I need to convert this method, in such a way that it should accept all Type of Objects(now i am accepting only Objects of type "BOEod").

As i am newbie to .Net so don no exactly how to use Generics. Can i accomplish this using Generics.?

Solution Something like this:

public T CheckCommandStatus<T>(T pBO, Ilist<string> pProperties){..}

Here Main thing is i need to change the Property of the Passed Object(pBO) and return it.


You would need BOEod to implement an interface which defines IsValid.

You would then add a generic constraint to your method to only accept objects implementing that interface.

  public interface IIsValid
  {
      bool IsValid{get;set;}
  }

....

  public class BOEod : IIsValid
  {
      public bool IsValid{get;set;}
  }

....

public T CheckCommandStatus<T>(T pBO, IList<string> pProperties) 
where T : IIsValid{..}


public BOEod CheckCommandStatus<T>(T pBo, IList<string> pProperties) where T : IBOEod
{
    pBo.isValid = false;
    if (pProperties != null)
    {
        int Num = -1;
        string propValue = pBo.GetType().GetProperty(pProperties[0].ToString()).GetValue(pBo, null).ToString();
        if (ifIntegerGetValue(propValue, out Num))
        {
            if (Num == 1)
                pBo.isValid = true;
        }
    }
    return pBo;
}

public interface IBOEod
{
    bool IsValid { get; set; }
}

All the types you want to pass to this method must implement the IBOEod interface.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜