简体   繁体   English

如何通过C#中的参数获取属性的对象实例?

[英]How can I get a property's object instance through a parameter in C#?

How can I get a property's object instance through a parameter in C#? 如何通过C#中的参数获取属性的对象实例? I'm not sure if it's called a variable instance, but here's what I meant: 我不确定是否将其称为变量实例,但这是我的意思:

In the usual case, when we do this, we get the value of the variable in C#: 在通常情况下,执行此操作时,将获得C#中变量的值:

void performOperation(ref Object property) {
   //here, property is a reference of whatever was passed into the variable
}

Pet myPet = Pet();
myPet.name = "Kitty";
performOperation(myPet.name);   //Here, what performOperation() will get is a string

What I hope to achieve, is to get the object from the property of the class, like say: 我希望实现的是从类的属性中获取对象,例如:

void performOperation(ref Object property) {
   //so, what I hope to achieve is something like this:

   //Ideally, I can get the Pet object instance from the property (myPet.name) that was passed in from the driver class
   (property.instance().GetType()) petObject = (property.instnace().GetType())property.instance();  

    //The usual case where property is whatever that was passed in. This case, since myPet.name is a string, this should be casted as a string
   (property.GetType()) petName = property;   
}

Pet myPet = Pet();
myPet.name = "Kitty";
performOperation(myPet.name);   //In this case, performOperation() should be able to know myPet from the property that was passed in

The instance() is just a dummy method to demonstrate that I want to get the property's instance object. instance()只是一个虚拟方法,用以演示我要获取属性的实例对象。 I am very new to C#. 我是C#的新手。 This is conceptually what I wish to achieve but I am not sure how I could do so in C#. 从概念上讲,这是我希望实现的目标,但是我不确定如何在C#中实现。 I looked through the Reflection API but I'm still not very sure what I should use to do this. 我浏览了Reflection API,但仍然不确定应该使用什么功能。

So, how can I get a property's object instance through a parameter in C#? 那么,如何通过C#中的参数获取属性的对象实例?

When you pass a property value to a method, for example: 将属性值传递给方法时,例如:

SomeMethod(obj.TheProperty);

Then it is implemented as: 然后将其实现为:

SomeType foo = obj.TheProperty;
SomeMethod(foo);

You cannot get the parent object from that, basically. 基本上,您不能从中获得父对象。 You would need to pass that separately, for example: 您将需要分别传递它,例如:

SomeMethod(obj, obj.TheProperty);

Additionally, keep in mind that a value can be part of any number of objects. 此外,请记住,值可以是任意数量的对象的一部分。 A string instance could be used in zero, one, or "many" objects. 字符串实例可用于零个,一个或“许多”对象中。 What you ask is fundamentally not possible. 您所要求的基本上是不可能的。

暂无
暂无

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

相关问题 我如何通过c#中的函数动态获取属性? - How i can dynamically get a Property through a function in c#? 使用C#,如何使用参数访问VB类的默认属性的getter / setter? - Using C#, how can I access the getter/setter of a VB class's default property with parameter? 如何从可执行文件中获取图标只有C#中的Process实例 - How can I get the icon from the executable file only having an instance of it's Process in C# 如何在array [i](C#)中访问对象的属性 - How to access an object's property at array[i] (C#) 如何在C#中的方法调用中传递对象实例? - How can I pass an object instance in a method call in C#? C#泛型如何获取没有对象类型作为参数的泛型方法的参数类型? - C# Generics How Can I get the type passed as an argument for a generic method with no object type as a parameter? 如何使用 HotChocolate C# 通过 GrahpQl 中的特定参数获取 object - How can I get object by specific parameter in GrahpQl with HotChocolate C# 如何通过附加的属性获取TextProperty的绑定对象 - How can I get the binding object of TextProperty through an attached Property 调用c#对象属性值“ get”时如何设置 - How can I set c# object property value when its “get” is called 如何从对象中获取属性和值的名称并将此名称和值传递给 c# 中的列表 - How can i get the name of property and value from object and pass this name and value to a list in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM