110,533
社区成员
发帖
与我相关
我的任务
分享
public sealed class Target
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="Assembly">程序集</param>
/// <param name="Target">对象</param>
/// <param name="Path">目标路径</param>
public Target(Assembly Assembly, string Target, string Path)
{
this.Target = Activator.CreateInstance(Assembly.GetType(Target));
string[] paths = Path.Split('.');
Method = paths[0];
_method = 0;
if (this.Target.GetType().GetProperty(Method) != null)
{
_method = 1;
}
if (this.Target.GetType().GetField(Method) != null)
{
_method = 2;
}
if (_method == 0)
throw new NullReferenceException("该对象不存在该属性");
if (paths.Count() > 1)
{
subpath = new Target(this, Path.Substring(Path.IndexOf('.')+1));
}
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="Target">对象</param>
/// <param name="Path">目标路径</param>
public Target(object Target, string Path)
{
this.Target = Target;
string[] paths = Path.Split('.');
Method = paths[0];
_method = 0;
if (Target.GetType().GetProperty(Method) != null)
{
_method = 1;
}
if (Target.GetType().GetField(Method) != null)
{
_method = 2;
}
if (_method == 0)
throw new NullReferenceException("该对象不存在该属性");
if (paths.Count() > 1)
{
subpath = new Target(this, Path.Substring(Path.IndexOf('.')+1));
}
}
private Target(MagicianTargetPath Parent, string Path)
{
parent = Parent;
Target = Parent.Value;
string[] paths = Path.Split('.');
Method = paths[0];
_method = 0;
if (Target.GetType().GetProperty(Method) != null)
{
_method = 1;
}
if (Target.GetType().GetField(Method) != null)
{
_method = 2;
}
if (_method == 0)
throw new NullReferenceException("该对象不存在该属性");
if (paths.Count() > 1)
{
subpath = new Target(this, Path.Substring(Path.IndexOf('.')+1));
}
}
private MagicianTargetPath parent;
private MagicianTargetPath subpath;
private int _method;
private object Target { get; set; }
private string Method { get; set; }
private object ThisValue
{
get
{
if (parent != null)
Target = parent.ThisValue;
if (_method == 1)
return Target.GetType().GetProperty(Method).GetValue(Target, null);
return Target.GetType().GetField(Method).GetValue(Target);
}
set
{
if (parent != null)
Target = parent.ThisValue;
if (_method == 1)
{
Target.GetType().GetProperty(Method).SetValue(Target, value, null);
}
else
{
Target.GetType().GetField(Method).SetValue(Target, value);
}
if (parent != null)
parent.ThisValue = Target;
}
}
/// <summary>
/// 对象值
/// </summary>
public object Value
{
get
{
if (subpath != null)
return subpath.Value;
return ThisValue;
}
set
{
if (subpath != null)
subpath.Value = value;
else
ThisValue = value;
}
}
}
class A
{
public Location Test{get;set;}
}
class B
{
public B( ref int Test)
{
Test = 123;
}
private int test;
public void Test()
{
test = 345;
}
}
void xxx()
{
var a = new a();
var b = new b(a.Test.X);
b.Test();
}
class B
{
public B(Target target)
{
this.target = target;
target.Value = 123;
}
private Target target;
public void Test()
{
target.Value = 345;
}
}
void xxx()
{
var a = new a();
var b = new b(new Target(a, "Test.X"));
b.Test();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var a = new A();
var b = new B(() => 345);
Console.WriteLine(b);
b.Test();
Console.WriteLine(b);
}
}
class A
{
public Location Test { get; set; }
}
class Location
{
public int X { get; set; }
}
class B
{
private Func<int> _SetTest;
public B(Func<int> SetTest)
{
_SetTest = SetTest;
test = 123;
}
private int test;
public void Test()
{
test = _SetTest();
}
public override string ToString()
{
return test.ToString();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Container c = new Container();
Console.WriteLine(c);
MyClass mycalss = c.MyClassInstance;
mycalss.X = 20;
c.MyClassInstance = mycalss;
Console.WriteLine(c);
}
}
class Container
{
public MyClass MyClassInstance { get; set; }
public Container() { MyClassInstance = new MyClass() { X = 1 }; }
public override string ToString()
{
return MyClassInstance.X.ToString();
}
}
struct MyClass
{
public int X { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Container c = new Container();
Console.WriteLine(c);
c.MyClassInstance.X = 20;
Console.WriteLine(c);
}
}
class Container
{
public MyClass MyClassInstance { get; set; }
public Container() { MyClassInstance = new MyClass() { X = 1 }; }
public override string ToString()
{
return MyClassInstance.X.ToString();
}
}
class MyClass
{
public int X { get; set; }
}
}