简体   繁体   English

如何在C#中对对象类型进行强制转换?

[英]How do I do a cast to an object type in C#?

I have a: 我有一个:

public class MyClass { 
 private string PrivateString; 
}

and I have an interface that accepts: 我有一个接受:

object o

I have a list of MyClass that I need to push through object o, and my Google is failing me. 我有一个MyClass列表,我需要通过对象o,我的谷歌让我失望。 What's the procedure to make this work? 使这项工作的程序是什么? I know this must be crazy simple, but I've yet to do type casting in C#. 我知道这一定很简单,但我还没有在C#中进行类型转换。

MyClass my = new MyClass();
WhateverTheFunctionThatTakesObjectIs(my);

Everything will implicitly cast to object. 一切都会隐含地转向对象。

The List<MyClass> object can still be just passed to your interface implementation, because List can be implicitly cast down to object (as can all objects). List<MyClass>对象仍然可以传递给您的接口实现,因为List可以隐式地转换为object (就像所有对象一样)。 If the compiler complains because of the way you have your code set up (i have to say that because you haven't actually shown us any code) then you can explicitly cast it: 如果编译器因为您设置代码的方式而抱怨(我必须说因为您实际上没有向我们展示任何代码),那么您可以显式地转换它:

myInterfaceFunction((object) myList);

or: 要么:

myInterfaceFunction(myList as object);

If you want to cast the contents of the list and pass a List<object> when you already have a List<MyClass> then you can just do this: 如果你想要转换列表的内容并在已经有List<MyClass>时传递List<object> ,那么你可以这样做:

myInterfaceFunction(myList.Cast<object>().ToList());

(you can get rid of the ToList() if your interface function uses IEnumerable<object> instead of List<object> ). (如果您的接口函数使用IEnumerable<object>而不是List<object> ),则可以删除ToList( )。

Here is a quick example. 这是一个简单的例子。 This has a good illustration of my comment about explicitly casting to object - by default the call to MyFunction(myList) will go to the MyFunction(IEnumerable<object> blah) function - you have to explicitly cast it to force it to go to the MyFunction(object blah) function. 这很好地说明了我关于显式转换为object注释 - 默认情况下,对MyFunction(myList)的调用将转到MyFunction(IEnumerable<object> blah)函数 - 你必须明确地转换它以强制它转到MyFunction(object blah)函数。

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<MyObject> myList = new List<MyObject>();

            MyFunction(myList);
            MyFunction((object)myList);
            MyFunction(myList.Cast<object>().ToList());
            MyFunction(myList.Cast<object>());
        }


        public static void MyFunction(List<object> blah)
        {
            Console.WriteLine(blah.GetType().ToString());
        }

        public static void MyFunction(IEnumerable<object> blah)
        {
            Console.WriteLine(blah.GetType().ToString());
        }

        public static void MyFunction(object blah) 
        {
            Console.WriteLine(blah.GetType().ToString());
        }
    }


    public class MyObject { }
}

您不需要进行任何类型转换以将MyClass对象用于获取对象参数的方法,但是您可能在将MyClass对象列表转换为对象列表时遇到问题

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

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