如何使用字符串变量调用子类?

发布于 2025-02-02 05:36:28 字数 527 浏览 2 评论 0原文

因此,我有一个简单的循环,我想访问类的变量

,但是有多个类,所以这就是我最终出现的循环的方式。

这就是我的代码的样子。

for (int i = 0; i<itemId.Length; i++)
{
    Type type = Type.GetType(" " + itemid[i]);
    object instance = Activator.CreateInstance(type);
    Debug.Log(itemResponse.data.Invoke(itemid[i]).name);
}

我正在尝试访问

itemResponse.data."My String".name

要访问的课程。

公共类_1001 {}
公共类_1002 {}
公共类_1003 {}

等等,我有什么办法可以做到吗?

谢谢

So I have this simple for loop and I want to access a variable of a class

However there are multiple classes so that's how I ended up with for loop.

This is what my code looks like.

for (int i = 0; i<itemId.Length; i++)
{
    Type type = Type.GetType(" " + itemid[i]);
    object instance = Activator.CreateInstance(type);
    Debug.Log(itemResponse.data.Invoke(itemid[i]).name);
}

I am trying to access

itemResponse.data."My String".name

The classes I want to access are.

public class _1001{ }
public class _1002{ }
public class _1003{ }

and so on, is there any way I could do that?

Thanks

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

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

发布评论

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

评论(2

浮云落日 2025-02-09 05:36:28

我不建议这样做,但是如果您不想创建建议的IVSoftware这样的界面,则此功能应该执行您想要的操作。

    static object AccessPropertyWithReflections(object obj, string propertyName)
    {
        object output = null;
        var typeInfo = obj.GetType();
        var propertyInfo = typeInfo.GetProperty(propertyName);
        if (propertyInfo is not null)
        {
            output = propertyInfo.GetValue(obj);
        }
        return output;
    }

I don't recommend doing this, but if you don't want to create an interface like IVSoftware recommended, this function should do what you want.

    static object AccessPropertyWithReflections(object obj, string propertyName)
    {
        object output = null;
        var typeInfo = obj.GetType();
        var propertyInfo = typeInfo.GetProperty(propertyName);
        if (propertyInfo is not null)
        {
            output = propertyInfo.GetValue(obj);
        }
        return output;
    }
青春有你 2025-02-09 05:36:28

面向对象的编程概念

这是我能想到的最基本的演示。我已经在帖子中命名的三个类,但是还定义了一个常见的界面。

interface ICommon
{
    void LogData();
}
class _1001 : ICommon
{
    public void LogData() => 
        Console.WriteLine("Class-specific datalogging for 'class _1001'.");
}
class _1002 : ICommon
{
    public void LogData() => 
        Console.WriteLine("Class-specific datalogging for 'class _1002'.");
}
class _1003 : ICommon    
{
    public void LogData() => 
        Console.WriteLine("Class-specific datalogging for 'class _1003'.");
}

这些类中的每一个都将以特定于类的方式实现icommon,尤其是logdata方法。即使我们将它们实例化为object

    object[] unknownObjects = new object[]
    {
        new _1001(),
        new _1002(),
        new _1003(),
    };

该接口允许在循环object []

    // Demonstrate implicit casting to an interface
    foreach (ICommon knownInterface in unknownObjects)
    {
        knownInterface.LogData();
    }
}

基本上允许隐式铸造的情况下, 这也可以。在循环中重新提供它将具有称为logdata的方法。然后,它继续调用正确的班级实现,而不会大惊小怪并打扰。而且,如果您要使用不实现icommon的对象进行尝试,则在运行时会获得invalidcastException

循环的输出为:

”在此处输入图像说明”

The Object-Oriented Programming concept of Polymorphism can simplify solving your question. It's what's "under the hood" for the very excellent comment about implementing an interface.

Here's the most basic demo I can think of. I've made the three classes that you name in your post, but in addition have defined a common interface.

interface ICommon
{
    void LogData();
}
class _1001 : ICommon
{
    public void LogData() => 
        Console.WriteLine("Class-specific datalogging for 'class _1001'.");
}
class _1002 : ICommon
{
    public void LogData() => 
        Console.WriteLine("Class-specific datalogging for 'class _1002'.");
}
class _1003 : ICommon    
{
    public void LogData() => 
        Console.WriteLine("Class-specific datalogging for 'class _1003'.");
}

Each of these classes will implement the ICommon and in particular the LogData method in its own class-specific way. This holds even if we instantiate them as type object:

    object[] unknownObjects = new object[]
    {
        new _1001(),
        new _1002(),
        new _1003(),
    };

The interface allows for implicit casting when looping the object[]

    // Demonstrate implicit casting to an interface
    foreach (ICommon knownInterface in unknownObjects)
    {
        knownInterface.LogData();
    }
}

Basically, you're promising the compiler that the object you're giving it in the loop will have a method called LogData. It then proceeds to call the correct class-specific implementation without a lot of fuss and bother. And if you were to attempt it with an object that doesn't implement ICommon you would get an InvalidCastException at runtime.

The output of the loop is:

enter image description here

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