【问题标题】:How to cast Object to its actual type and convert it?如何将 Object 转换为其实际类型并进行转换?
【发布时间】:2016-05-03 01:48:34
【问题描述】:

如何将对象数量放入_StoredStock?数量是一个对象,我发现很难将其转换为 int 以将值放入 _StoredStock

class EachStock: Stock
{

    public override void bookStock(Quantity quantity)
    {
        if (quantity.GetType() == typeof(EachStockQuantity))
        {
            //How to placethe object quantity into a _BookedStock
            // the quantity is an object which I find it hard to convert it to int for _StoredStock
        }
    }

}

【问题讨论】:

  • 能否也包括DiscreteStockQuantity的定义?
  • 我包含两个类
  • 如果我把 _StoredStock = Convert.ToInt32(quantity.ToString());我得到“在 mscorlib.dll 中发生类型为 'System.FormatException' 的未处理异常附加信息:输入字符串的格式不正确。”
  • 尝试实现转换运算符stackoverflow.com/questions/18784274/…

标签: c#


【解决方案1】:

您正试图将一个类实例存储在您的整数变量中。我认为部分问题在于参数名称具有误导性,因为它不是数量,而是具有 Quantity 属性的类。

访问您需要的属性,在本例中为Quantity

public override void AdjustStock(StockQuantity stockQty)
{
    if (stockQty.GetType() == typeof(DiscreteStockQuantity))
        _StoredStock = ((DiscreteStockQuantity)stockQty).Quantity;
}

另一种选择,并且可能更具可读性(但这是一个见仁见智的问题):

public override void AdjustStock(StockQuantity stockQty)
{
    var discStockQty = stockQty as DiscreteStockQuantity;

    if (discStockQty != null)
        _StoredStock = discStockQty.Quantity;
}

【讨论】:

  • 你是救生员@Grant :)
【解决方案2】:

有不同的方法可以知道类型并进行实际的转换。

这是一个懒惰的例子:

class Program
{
    static void Main(string[] args)
    {

        object obj = new Foo();


        Type objType = obj.GetType();

        if(objType == typeof(Foo))
        {
            var foobar = (Foo)obj;

            Console.WriteLine(foobar.Bar);
        }
        else if(objType == typeof(int))
        {
            // Do something
        }
        else
        {
            // Do something
        }

        // If you are brave enough you can use dynamic

        dynamic bar = obj;

        Console.WriteLine(bar.Bar);

        Console.ReadLine();
    }
}

class Foo
{
    public string Bar { get; set; }

    public Foo()
    {
        Bar = "FooBar";
    }
}

这个输出:

FooBar
FooBar

更新:

这是一个与您的问题类似的示例

class Program
{
    static void Main(string[] args)
    {
        int i = FooBar(new Foo());

        Console.WriteLine(i);

        Console.ReadLine();
    }

    public static int FooBar(FooBase fooBase)
    {
        int val = -1;

        Type type = fooBase.GetType();

        if(type == typeof(Foo))
        {
            Foo foo = (Foo)fooBase;

            val = foo.Bar;
        }
        else
        {
            // Do something
        }

        return val;
    }
}

class Foo : FooBase
{
    public int Bar { get; set; }

    public Foo()
    {
        Bar = 999;
    }
}

class FooBase
{

}

输出:

999

【讨论】:

    【解决方案3】:

    试试这个:

    public override void AdjustStock(StockQuantity quantity)
        {
            if (quantity is DiscreteStockQuantity)
            {
                var dq = quantity as DiscreteStockQuantity;
                _StoredStock = dq.Quantity;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2012-08-27
      • 1970-01-01
      • 2011-08-20
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      相关资源
      最近更新 更多