简体   繁体   English

如何将 Object 转换为其实际类型并进行转换?

[英]How to cast Object to its actual type and convert it?

How to place the object quantity into a _StoredStock ?如何将对象数量放入_StoredStock The quantity is an object which I find it hard to convert it to int to place the value into _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
        }
    }

} }

You're trying to store a class instance in your integer variable.您正在尝试将类实例存储在整数变量中。 I think part of the issue is that the parameter name is misleading, as it's not a quantity, but a class with a Quantity property.我认为部分问题在于参数名称具有误导性,因为它不是数量,而是具有 Quantity 属性的类。

Access the property you need, in this case Quantity :访问您需要的属性,在本例中为Quantity

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

Alternatively, and possibly slightly more readable (but that's a matter of opinion):或者,可能稍微更具可读性(但这是一个意见问题):

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

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

There are different ways to know the type and do the actual conversion.有多种方法可以了解类型并进行实际转换。

Here is a lazy example:这是一个懒惰的例子:

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";
    }
}

This outputs:这输出:

FooBar
FooBar

Update:更新:

Here's an example similar to your problem这是一个类似于您的问题的示例

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
{

}

Outputs:输出:

999

Try this:尝试这个:

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

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

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