class Program { static void Main(string[] args) { ClassA a = new ClassA(); ClassB b = new ClassB(); a.title = "test"; object obj = a; Method(ref obj,"title"); string new_title = a.title; } public static void Method(ref object obj,string propName) { obj.GetType().GetProperty(propName).SetValue(obj, "dev"); } } public class ClassA { public string title{get;set;} public int count{get;set;} } public class ClassB { public decimal money{get;set;} public DateTime date{get;set;} }
这里用out并不合适(out的参数一开始会被初始化)
public static void Method(ref object obj,string propName) { var temp= obj.GetType().GetProperty(propName).GetValue(obj); 这时候的temp就是你的 tile的值,对它一系列的操作后 temp+=“1”; 重新赋值给obj obj.GetType().GetProperty(propName).SetValue(obj, temp); }
泛型方法看看。 private T Method<T>(out T obj) where T : new() { obj = new T(); return obj; }int obj = 1; var obj1 = Method<int>(out obj);
interface ICalculate { Calc(R r); }
public void Method(ICalculate obj)
{
obj.Calc(R);
}
{
static void Main(string[] args)
{
ClassA a = new ClassA();
ClassB b = new ClassB();
a.title = "test"; object obj = a;
Method(ref obj,"title"); string new_title = a.title;
} public static void Method(ref object obj,string propName)
{
obj.GetType().GetProperty(propName).SetValue(obj, "dev");
}
} public class ClassA
{
public string title{get;set;}
public int count{get;set;}
}
public class ClassB
{
public decimal money{get;set;}
public DateTime date{get;set;}
}
{
var temp= obj.GetType().GetProperty(propName).GetValue(obj);
这时候的temp就是你的 tile的值,对它一系列的操作后
temp+=“1”;
重新赋值给obj
obj.GetType().GetProperty(propName).SetValue(obj, temp);
}
private T Method<T>(out T obj) where T : new()
{
obj = new T(); return obj;
}int obj = 1;
var obj1 = Method<int>(out obj);