简体   繁体   English

C# How to acces to Values of an class Object, by an another Class object

[英]C# How to acces to Values of an class Object, by an another Class object

I want to assign an object of one class to another class by constructor, and retrieve the values of the class assigned by constructor from the class which has the constructor. I want to assign an object of one class to another class by constructor, and retrieve the values of the class assigned by constructor from the class which has the constructor. Sounds confusing, it is too:).听起来令人困惑,它也是:)。 Can't handle it.处理不了。 Do you have any ideas?你有什么想法? Here is an example code.这是一个示例代码。

public class Class1
{
   public class1(int value1)
   {
       Value1 = value1;
   }
   public int Value1 { get;}
}

public class Class2
{
   public object Object1 {get; set;}
}

public class main
{

   Class1 TestClass = new Class1(15);
   Class2 TestClass1 = new Class2();

   public void main()
   {
       TestClass1.Object1 = TestClass();

       //Now I want to this
       Console.WriteLine(TestClass1.Object1.Value1);
       TestClass1.Object1.Value1;   
   }
}

For that you need to creare property of that particular type instead of using base object type like:为此,您需要创建该特定类型的属性,而不是使用基本 object 类型,例如:

public class Class2
{
   public Class1 Object1 {get; set;}
}

then you can write:然后你可以写:

Class1 TestClass = new Class1(15);
Class2 TestClass1 = new Class2();

public void main()
{
   TestClass1.Object1 = TestClass;

   //Now you can do what you wanted
   Console.WriteLine(TestClass1.Object1.Value1);
}

if you really want to used object type then you will need to cast the object to it's actual type and then you can have the properties available at compile time:如果你真的想使用 object 类型,那么你需要将 object 转换为它的实际类型,然后你可以在编译时使用这些属性:

public class Class2
{
   public object Object1 {get; set;}
}

and now:现在:

public void main()
{
   TestClass1.Object1 = TestClass;

   Class1 temp =  TestClass1.Object1 as Class1;
   // safe guard in case cast fails 
   if(temp !=null)
   {
      //Now you can do what you wanted
      Console.WriteLine(temp.Value1);
   }
}

Class stores it's child as object and object doesn't have a property Value1 . Class将其子项存储为objectobject没有属性Value1

You need to cast the Object1 property or make Class2 generic.您需要Object1属性或使Class2通用。

Casting铸件

Class1 o1 = new Class1(15);
Class2 o2 = new Class2();

o2.Object1 = o1;

var x = o2.Object1 as Class1;
//If o2.Object1 is not Class1 x will be null.  

Console.WriteLine(x.Value1); 

Making Class2 generic使Class2通用

public class Class1
{
    public Class1(int value1) { Value1 = value1; }
    public int Value1 { get; set; }
}

public class Class2<T>
{
    public T Object1 { get; set; }
}

public static async Task Main()
{
    var o1 = new Class1(15);
    var o2 = new Class2<Class1>();

    o2.Object1 = o1;

    Console.WriteLine(o2.Object1.Value1);
}

Following approach take nested instance as a constructor parameter whilst constructing outer / container instance.以下方法在构造外部/容器实例时将嵌套实例作为构造函数参数。

        public class Inner
        {
            public Inner(int value)
            {
                Value = value;
            }
            public int Value { get; }
        }

        public class Outer
        {
            public Outer(Inner inner)
            {
                InnerObject = inner;
            }
            public Inner InnerObject { get;}
        }

        public class main
        {       

            static void Main()
            {
                var outer = new Outer(new Inner(10));
                Console.WriteLine(outer.InnerObject.Value);
                var value = outer.InnerObject.Value;
            }
        }

Try these two object extension methods for getting and setting values.尝试这两种 object 扩展方法来获取和设置值。

Usage;用法;

myObject.SetPropertyByName("MyProperty", "MyValue"); myObject.SetPropertyByName("MyProperty", "MyValue");

var value = myObject.GetPropertyValueByName("MyProperty"); var value = myObject.GetPropertyValueByName("MyProperty");

public static class FwExtensions
{
    /// <summary>
    /// Set the value of a property if it exists and you don't have an interface
    /// </summary>
    /// <param name="me"></param>
    /// <param name="propertyName"></param>
    /// <param name="value"></param>
    /// <returns> true if the value was found and set, else false</returns>
    public static bool SetPropertyByName(this object me, string propertyName, object value)
    {
        if (me != null)
        {
            PropertyInfo prop = me.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance);
            if (null != prop && prop.CanWrite)
            {
                prop.SetValue(me, value, null);
                return true;
            }
        }
        return false;
    }

    public static object GetPropertyValueByName(this object me, string propertyName)
    {
        if (me != null)
        {
            PropertyInfo prop = me.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance);
            if (null != prop && prop.CanWrite)
            {
                return prop.GetValue(me);
            }
        }
        return null;
    }
}

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

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