C#,如何将对象转换为类类型

发布于 2025-01-15 10:36:46 字数 677 浏览 0 评论 0原文

我正在开发 .NET CORE 应用程序。我已经声明了一个 Object ,我需要将其转换或转换为 Customer 类类型。我有一个场景,基于 bool 值,我需要更改类型并返回该类型。

错误

System.InvalidCastException: 'Object must implement IConvertible

客户类

public class Customer
{
    public string Name { get; set; }

    public Customer GetCutsomer(){
             //
    }
}

“对象转换”

public class MyService
{
   public void CastType(){

       Customer obj = new Customer();

       var cus = GetCutsomer();

       Object customer = new Object();

       Convert.ChangeType(customer , cus.GetType());
   }
}

I am working on .NET CORE application. I have declare an Object that I need to cast or convert to Customer class type. I have scenario where based on bool value I need to change the type and return that.

error

System.InvalidCastException: 'Object must implement IConvertible

Customer Class

public class Customer
{
    public string Name { get; set; }

    public Customer GetCutsomer(){
             //
    }
}

'Object Casting`

public class MyService
{
   public void CastType(){

       Customer obj = new Customer();

       var cus = GetCutsomer();

       Object customer = new Object();

       Convert.ChangeType(customer , cus.GetType());
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感性不性感 2025-01-22 10:36:46

你有一些选择。让我们创建一个 Customer 并通过分配给一个对象变量来释放这个事实。也不是客户来显示这种情况

    object o1 = new Customer();
    object o2 = new String("not a customer");

,所以现在我们想找回其“客户”性

首先我们可以使用 as

    Customer as1 = o1 as Customer;
    Customer as2 = o2 as Customer;
  • as1 最终作为有效的客户指针
  • as2 最终 null 因为 o2 不是 Customer 对象

或者我们可以进行强制转换,

    var cast1 = (Customer)o1;
    try {
        var cast2 = (Customer)o2;
    }
    catch {
        Console.WriteLine("nope");
    }
  • 第一个成功,
  • 第二个抛出 InvalidCast 异常

我们还可以使用 is 询问该对象

    if (o1 is Customer)
        Console.WriteLine("yup");
    if (o2 is Customer)
        Console.WriteLine("yup");

这也有效(回答问题在评论中)

object o1 = new Customer();

现在

Customer c = (Customer)o1;

然后稍后

o1 = new Order();
...
Order ord1 = (Order)o1;

即对象指针可以指向任何对象。

这就是继承的作用。 C# 中的所有类对象最终都派生自 Object,只是您在代码中看不到它。所以对象指针可以指向任何类对象

You have some choices. Lets make a Customer and loose that fact by assigning to an object variable. And one that isnt a customer to show that case too

    object o1 = new Customer();
    object o2 = new String("not a customer");

so now we want to get back its 'customer'ness

First we can use as

    Customer as1 = o1 as Customer;
    Customer as2 = o2 as Customer;
  • as1 ends up as a valid Customer pointer
  • as2 ends up null since o2 is not a Customer object

Or we can do a cast

    var cast1 = (Customer)o1;
    try {
        var cast2 = (Customer)o2;
    }
    catch {
        Console.WriteLine("nope");
    }
  • the first one succeeds
  • the second one throws and InvalidCast exception

We can also ask about the object using is

    if (o1 is Customer)
        Console.WriteLine("yup");
    if (o2 is Customer)
        Console.WriteLine("yup");

This works too (answering question in comment)

object o1 = new Customer();

now

Customer c = (Customer)o1;

then later

o1 = new Order();
...
Order ord1 = (Order)o1;

Ie an Object pointer can point at any object.

This is inheritance at work. All class objects in c# are ultimately derived from Object, you just dont see it in your code. So an Object pointer can point at any class object

一口甜 2025-01-22 10:36:46

您应该能够安全地将任何对象转换为类:

var obj = new Object();
var customer = (Customer)obj;

You should simply be able to safely cast any object to a class:

var obj = new Object();
var customer = (Customer)obj;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文