none
Using Reflection Need Property Fields in another Class Property. RRS feed

  • Question

  • Using Reflection Need Property Fields in another Class Property.

     public class Test 
        {
    	  public PasswordModel passwordModel { get; set; }
    	  
    	   public Test()
            {
                passwordModel = PasswordContext?.passwordModel
            }
    	}
    	
    	public class PasswordModel : DomainModel
        {
            public PasswordModel() { }
    
            public PasswordModel(ICSGContext ctx)
    
            private SEUIDataMember<string> _tt1;
            public SEUIDataMember<string> Tt1 => TranslateDomain.StringAttr(DomainObject, Ignore, "Tt1", "Tt1", ref _tt1);
    
            private SEUIDataMember<string> _tt2;
            public SEUIDataMember<string> Tt2 => TranslateDomain.StringAttr(DomainObject, Ignore, "Tt2", "Tt2", ref _tt2);
    
        }
    	
    	
    	 public class Test2 
        {
    	  public PasswordModel passwordModel { get; set; }
    	  
    	    public Test2()
            {
                Without Creating Instance Test, I need passwordModel from Test to Test2
            }
    	}
    	
    	I need passwordModel all fields to Test2 without creating instance of Test using Reflection



    • Edited by ID GO Monday, May 27, 2019 6:15 PM
    Monday, May 27, 2019 6:01 PM

All replies

  • Hi ID GO,

    Thank you for posting here.

    Based on your description, you want to get property fields by using reflection.

    You could try the following code.

     public class Test
        {
            public PasswordModel passwordModel { get; set; }
    
            public string Name { get; set; }
            public int Age { get; set; }
            public Test()
            {
            }
        }
     public class Test2
        {
            public PasswordModel passwordModel { get; set; }
    
            public Test2()
            {
                //Without Creating Instance Test, I need passwordModel from Test to Test2
                Type t = typeof(Test);
                PropertyInfo[] fieldInfo = t.GetProperties();
                foreach (var item in fieldInfo)
                {
                    Console.WriteLine(item.Name);
                }
            }
        }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 28, 2019 3:19 AM
    Moderator
  • I need to get the Value of the Model : passwordModel 

    passwordModel : contains Tt1 & Tt2 Value.

    Tuesday, May 28, 2019 5:20 AM
  • Hi ID GO,

    Thanks for the feedback.

    For your last reply, you want to get the value of the Model(passwordModel). Meanwhile, you don't want to create Instance Test. I think it is hard to achieve it.

    Because if we don't create an instance for a class, they will not have the corresponding value.

    For example, we create a class called Student, it has two properties: Name and Age, If we don't create instance for it, it will not have the value of Age and Name.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 28, 2019 6:44 AM
    Moderator