我想通过Reflection设置对象的属性,值为type string.所以,举个例子,假设我有Ship一个属性为的类Latitude,它是一个double.
这是我想做的事情:
Ship ship = new Ship();
string value = "5.5";
PropertyInfo propertyInfo = ship.GetType().GetProperty("Latitude");
propertyInfo.SetValue(ship, value, null);
Run Code Online (Sandbox Code Playgroud)
这样就抛出了ArgumentException:
"System.String"类型的对象无法转换为"System.Double"类型.
如何将值转换为正确的类型,基于propertyInfo?
我问这个的原因是因为我被@Greg D(来自这个问题)的推荐用来SetCurrentValue()代替,但看看文档并没有看到差异.或者什么"不改变其价值来源"意味着什么呢?
设置依赖项属性的本地值,由其依赖项属性标识符指定.
设置依赖项属性的值而不更改其值源.
我在使用反射时遇到了性能问题.
所以我决定为我的对象的属性创建委托,到目前为止得到了这个:
TestClass cwp = new TestClass();
var propertyInt = typeof(TestClass).GetProperties().Single(obj => obj.Name == "AnyValue");
var access = BuildGetAccessor(propertyInt.GetGetMethod());
var result = access(cwp);
Run Code Online (Sandbox Code Playgroud)
static Func<object, object> BuildGetAccessor(MethodInfo method)
{
var obj = Expression.Parameter(typeof(object), "o");
Expression<Func<object, object>> expr =
Expression.Lambda<Func<object, object>>(
Expression.Convert(
Expression.Call(
Expression.Convert(obj, method.DeclaringType),
method),
typeof(object)),
obj);
return expr.Compile();
}
Run Code Online (Sandbox Code Playgroud)
结果非常令人满意,比使用传统方法快30-40倍(PropertyInfo.GetValue (obj, null);)
问题是:我怎样才能创建SetValue一个属性相同的属性?不幸的是没有办法.
我这样做是因为我不能使用方法,<T>因为我的应用程序的结构.
对于属性有GetGetMethod,GetSetMethod所以我可以这样做:
Getter = (Func<S, T>)Delegate.CreateDelegate(typeof(Func<S, T>),
propertyInfo.GetGetMethod());
Run Code Online (Sandbox Code Playgroud)
和
Setter = (Action<S, T>)Delegate.CreateDelegate(typeof(Action<S, T>),
propertyInfo.GetSetMethod());
Run Code Online (Sandbox Code Playgroud)
但我怎么去FieldInfos?
我不是在寻找代表GetValue和SetValue(这意味着我每次都会调用反射)
Getter = s => (T)fieldInfo.GetValue(s);
Setter = (s, t) => (T)fieldInfo.SetValue(s, t);
Run Code Online (Sandbox Code Playgroud)
但如果这里有CreateDelegate办法吗?我的意思是,因为赋值返回一个值,我可以将赋值视为一种方法吗?如果有的话有MethodInfo手柄吗?换句话说,我如何传递MethodInfo设置权并从成员字段获取值到CreateDelegate方法,以便我得到一个委托,我可以直接读取和写入字段?
Getter = (Func<S, T>)Delegate.CreateDelegate(typeof(Func<S, T>), fieldInfo.??);
Setter = (Action<S, T>)Delegate.CreateDelegate(typeof(Action<S, T>), fieldInfo.??);
Run Code Online (Sandbox Code Playgroud)
我可以构建表达式并编译它,但我正在寻找更简单的东西.最后,如果问题没有答案,我不介意去表达路线,如下所示:
var instExp = Expression.Parameter(typeof(S));
var fieldExp = Expression.Field(instExp, fieldInfo);
Getter = Expression.Lambda<Func<S, T>>(fieldExp, instExp).Compile(); …Run Code Online (Sandbox Code Playgroud) 在Swift中,它不可能使用 .setValue(..., forKey: ...)
Int?enum类型的属性[MyObject?] 这有一个解决方法,即通过覆盖对象本身中的setValue forUndefinedKey方法.
因为我正在编写一个基于反射的通用对象映射器.请参阅EVReflection我希望尽可能地减少这种手动映射.
还有其他方法可以自动设置这些属性吗?
解决方法:可以在我的库中的单元测试发现这里 这是代码:
class WorkaroundsTests: XCTestCase {
func testWorkarounds() {
let json:String = "{\"nullableType\": 1,\"status\": 0, \"list\": [ {\"nullableType\": 2}, {\"nullableType\": 3}] }"
let status = Testobject(json: json)
XCTAssertTrue(status.nullableType == 1, "the nullableType should be 1")
XCTAssertTrue(status.status == .NotOK, "the status should be NotOK")
XCTAssertTrue(status.list.count == 2, "the list should have 2 items")
if status.list.count == 2 {
XCTAssertTrue(status.list[0]?.nullableType == 2, "the …Run Code Online (Sandbox Code Playgroud) 尝试理解传递给setValue时'onlySelf'参数的作用.
this.form.get('name').setValue('', { onlySelf: true })
Run Code Online (Sandbox Code Playgroud)
文档说:"如果onlySelf为true,则此更改只会影响此FormControl的验证,而不会影响其父组件.默认为false."
但是我很难理解这一点.使用Angulars的模型驱动形式仍然相当新.
在以前的项目中的各个地方使用带有此代码的Copy方法(处理具有相同命名属性但不从公共基类派生或实现公共接口的对象).
新的工作场所,新的代码库 - 现在它在SetValue失败,"对象与目标类型不匹配",即使是非常简单的例子......它在上周工作了....
public static void Copy(object fromObj, object toObj)
{
Type fromObjectType = fromObj.GetType();
Type toObjectType = toObj.GetType();
foreach (System.Reflection.PropertyInfo fromProperty in
fromObjectType.GetProperties())
{
if (fromProperty.CanRead)
{
string propertyName = fromProperty.Name;
Type propertyType = fromProperty.PropertyType;
System.Reflection.PropertyInfo toProperty =
toObjectType.GetProperty(propertyName);
Type toPropertyType = toProperty.PropertyType;
if (toProperty != null && toProperty.CanWrite)
{
object fromValue = fromProperty.GetValue(fromObj,null);
toProperty.SetValue(toProperty,fromValue,null);
}
}
}
}
private class test
{
private int val;
private string desc;
public int Val { get { return val; } …Run Code Online (Sandbox Code Playgroud) 我有一个具有文件上传字段的表单页面,我试图以编程方式设置该字段的值,虽然我知道由于安全原因它是不可能的,但我想知道我们是否仍然可以?如果有插件或我可以用来执行上传的东西.我有设置该字段所需的文件字段的Base64值,我需要知道的是有没有办法设置它.
在这方面,任何建议或帮助都会很棒.
谢谢.
更新: 添加了一个JsFiddle来演示我正在尝试的内容.
请访问此网站,使用js小提琴中的示例创建一个文本文件,并使用此链接进行转换.
var str = `text`;
Run Code Online (Sandbox Code Playgroud)
http://www.motobit.com/util/base64-decoder-encoder.asp
注意:
下面的答案反映了2009年遗留浏览器的状态.现在,您可以在2017年使用JavaScript动态/编程地设置文件输入元素的值.
有关详细信息和演示,请参阅此问题中的答案:
如何以编程方式设置文件输入值(即:拖放文件时)?
我试图做尽可能多的研究,但无法找到这个相当简单的问题的答案(想要在我设置SDK和所有内容之前解决这个问题.)我正在考虑开发我的第一个应用程序,我想知道是否BATTERY_STATUS_CHARGING从BatteryManager只包含一个get函数或也是一个set功能.
我不想制作一个应用程序,我可以手动停止手机充电,而无需从充电器上拔下电源(通过USB),所以我想知道这是否真的可行.
我有很多用户数据的文本字段.我希望从DB设置它.
items: [{
xtype: "form",
bodyPadding: 5,
border: false,
defaults: {
xtype: "textfield",
inputType: "text",
anchor: "100%"
},
items: [{
fieldLabel: 'Username:',
readOnly: true,
value: 'Admin',
name: "username"
}, {
Run Code Online (Sandbox Code Playgroud)
我有一个openDB.js用方法命名的外部类,getUserByUsername()
这里是小代码,它将如何在其他视图中使用,它可以工作,但在我的实际视图中,我无法设置文本字段的值.请帮忙怎么做?
openDB.getUserByUsername(user.username).then(function(userDetails) {
me.setTitle("Welcome " + userDetails.mail + "!");
});
Run Code Online (Sandbox Code Playgroud)
我想用这样的东西做这样的事 value: 'Admin'......
我在Sencha论坛找到了一些方法,但我可以使用吗?
setValue: function(value) {
var me = this;
me.setRawValue(me.valueToRaw(value));
return me.mixins.field.setValue.call(me, value);
},
Run Code Online (Sandbox Code Playgroud) setvalue ×10
c# ×4
reflection ×4
action ×1
android ×1
angular ×1
battery ×1
delegates ×1
expression ×1
extjs ×1
fieldinfo ×1
file-upload ×1
forms ×1
html ×1
javascript ×1
jquery ×1
propertyinfo ×1
set ×1
swift ×1
textfield ×1
wpf ×1