标签: setvalue

通过反射使用字符串值设置属性

我想通过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

c# reflection type-conversion propertyinfo setvalue

298
推荐指数
8
解决办法
26万
查看次数

Dependency Property SetValue()和SetCurrentValue()之间的区别是什么

我问这个的原因是因为我被@Greg D(来自这个问题)的推荐用来SetCurrentValue()代替,但看看文档并没有看到差异.或者什么"不改变其价值来源"意味着什么呢?

SetValue()

设置依赖项属性的本地值,由其依赖项属性标识符指定.

SetCurrentValue()

设置依赖项属性的值而不更改其值源.

wpf dependency-properties setvalue setcurrentvalue

49
推荐指数
1
解决办法
1万
查看次数

反射性能 - 创建代理(属性C#)

我在使用反射时遇到了性能问题.
所以我决定为我的对象的属性创建委托,到目前为止得到了这个:

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>因为我的应用程序的结构.

c# reflection expression action setvalue

25
推荐指数
2
解决办法
1万
查看次数

有没有办法创建一个委托来获取和设置FieldInfo的值?

对于属性有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?

我不是在寻找代表GetValueSetValue(这意味着我每次都会调用反射)

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)

c# delegates expression-trees setvalue fieldinfo

21
推荐指数
4
解决办法
9836
查看次数

使用反射设置对象属性而不使用setValue forKey

在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)

reflection setvalue swift

19
推荐指数
1
解决办法
6727
查看次数

Angular 2 - FormControl setValue'only'Self'参数

尝试理解传递给setValue时'onlySelf'参数的作用.

this.form.get('name').setValue('', { onlySelf: true })
Run Code Online (Sandbox Code Playgroud)

文档说:"如果onlySelf为true,则此更改只会影响此FormControl的验证,而不会影响其父组件.默认为false."

但是我很难理解这一点.使用Angulars的模型驱动形式仍然相当新.

forms setvalue angular

19
推荐指数
2
解决办法
2万
查看次数

PropertyInfo实例上的SetValue错误"对象与目标类型不匹配"c#

在以前的项目中的各个地方使用带有此代码的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)

c# reflection setvalue

16
推荐指数
2
解决办法
2万
查看次数

设置文件上载字段值 - Javascript/jQuery

我有一个具有文件上传字段的表单页面,我试图以编程方式设置该字段的值,虽然我知道由于安全原因它是不可能的,但我想知道我们是否仍然可以?如果有插件或我可以用来执行上传的东西.我有设置该字段所需的文件字段的Base64值,我需要知道的是有没有办法设置它.

在这方面,任何建议或帮助都会很棒.

谢谢.

更新: 添加了一个JsFiddle来演示我正在尝试的内容.

请访问此网站,使用js小提琴中的示例创建一个文本文件,并使用此链接进行转换.

var str = `text`;
Run Code Online (Sandbox Code Playgroud)

http://www.motobit.com/util/base64-decoder-encoder.asp

的jsfiddle

注意:

下面的答案反映了2009年遗留浏览器的状态.现在,您可以在2017年使用JavaScript动态/编程地设置文件输入元素的值.

有关详细信息和演示,请参阅此问题中的答案:
如何以编程方式设置文件输入值(即:拖放文件时)?

html javascript jquery file-upload setvalue

15
推荐指数
2
解决办法
5456
查看次数

如何通过USB以编程方式停止手机充电

我试图做尽可能多的研究,但无法找到这个相当简单的问题的答案(想要在我设置SDK和所有内容之前解决这个问题.)我正在考虑开发我的第一个应用程序,我想知道是否BATTERY_STATUS_CHARGINGBatteryManager只包含一个get函数或也是一个set功能.

我不想制作一个应用程序,我可以手动停止手机充电,而无需从充电器上拔下电源(通过USB),所以我想知道这是否真的可行.

android battery setvalue

13
推荐指数
3
解决办法
3万
查看次数

如何设置extjs textfield的值?

我有很多用户数据的文本字段.我希望从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)

extjs set textfield setvalue

11
推荐指数
1
解决办法
6万
查看次数