我有一个构造对象,我想按名称和值注入属性。 有没有办法使用Unity / Castle windsor /另一个框架?
答案 0 :(得分:0)
以下是如何使用.NET实现的:
var myObject = new MyObject();
// do something
var fields = typeof(MyObject).GetFields();
foreach (var field in fields) {
// this is the name of the field itself
var name = field.Name;
// this is the current value of the field
var val = field.GetValue(myObject );
// this sets the current field to the newValue
var newValue = 0;
field.SetValue(myObject, newValue);
}
答案 1 :(得分:0)
例如,使用统一
IUnityContainer unityContainer = new UnityContainer();
unityContainer.RegisterType<IClassB, ClassB>();
使用弹簧
http://springframework.net/doc-latest/reference/html/quickstarts.html
<object name="AnotherMovieFinder"
type="Spring.Examples.MovieFinder.ColonDelimitedMovieFinder, Spring.Examples.MovieFinder">
<constructor-arg index="0" value="movies.txt"/>
</object>
或使用反射,您可以实现从任何地方获取值