c# - Dynamically Access Properties by Type -


I am trying to use an asset which is of the same type as is given in general.

Look at the code:

  class custom class {customproperty property {get; Set;}} Category CustomProperty {} Main {// Create a new instance of my custom class Custom Class myClass = New CustomClass (); // Create a new example of another class which is the same as myClass.property's customproperty myProp = new custom property (); // Normal method Dynamic Aplipion Object & lt; Custom Property & gt; (MyProp, myClass); } Private Zero DynamicallyAccessPropertyOnObject & lt; T & gt; (It's T propertyToAccess, CustomClass class) {/ I want to use the property (in the class) that is the same type of common type (typeToAccess) / TODO: to reach the right property based on the type I got Need help}  

If you can not see the code, I want to be able to enter a general in something and then want to use the property on a square Which is the kind of inside Sector was passed.

Is there any good way to do this? If you need an explanation then I know ...

You can reflection, and LINQ :

Private static zero dynamic assetpropertyone object & lt; T & gt; () {Var customClass = typeof (CustomClass); Var Properties = Custom Class .GetProperties (). Before and after default (x => x.PropertyType == typef (t)); }

If you are only doing this for CustomClass , you can remove both parameters. Then you can call it:

  DynamicallyAccessPropertyOnObject & LT; CustomProperty & gt; ();  

If you want to make it normal, then use two common arguments:

  Private static zero dynamic AplicityOnBizet & lt; T, K & gt; (K targetObj) {var targetType = targetObj.GetType (); Var Properties = Goal Type GETProperties () Before and after default (x = & gt; x.PropertyType == type (t)); If (property! = Null) {var value = (t) property Gate value (targetObj); }}  

Then call it:

  Dynamic Avenuepropertyone object & lt; Custom Property, Custom Class & gt; (MyClass);  

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -