Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果可以将值转换为数字,则通过反射设置字符串属性将删除前导零_C#_Reflection_.net 3.5 - Fatal编程技术网

C# 如果可以将值转换为数字,则通过反射设置字符串属性将删除前导零

C# 如果可以将值转换为数字,则通过反射设置字符串属性将删除前导零,c#,reflection,.net-3.5,C#,Reflection,.net 3.5,我有一个处理与数据库通信的框架。它通过将对象的属性名与参数名匹配来自动填充参数,从而调用my SPs(搜索、ins、del、upd)。在我得到一个只包含数字的字符串值之前,一切都很顺利,第一个数字是零。代码会自动删除前导零,即使my属性和DB列都是字符串。 以下是我代码的一部分: var property = from p in entityProperties where p.Name.ToUpper().Equals(dvSchema[index][0].ToS

我有一个处理与数据库通信的框架。它通过将对象的属性名与参数名匹配来自动填充参数,从而调用my SPs(搜索、ins、del、upd)。在我得到一个只包含数字的字符串值之前,一切都很顺利,第一个数字是零。代码会自动删除前导零,即使my属性和DB列都是字符串。 以下是我代码的一部分:

var property = from p in entityProperties
               where p.Name.ToUpper().Equals(dvSchema[index][0].ToString().ToUpper())
               select p;
object propertyNewValue;
if (property.First().PropertyType.BaseType != typeof(Enum))
    propertyNewValue = dr[index];
...
property.First().SetValue(businessEntity, propertyNewValue, null);
我正在使用.NET3.5和SQLServer2008

除了添加if来检查属性类型是否为string之外,还有人知道如何避免这种情况吗

提前谢谢


编辑 以下是我的完整过程,我不会在任何地方显式强制转换属性:

    public virtual void MapEntityFromDataBase(BE businessEntity, IDataReader dr)
    {
        DataView dvSchema = dr.GetSchemaTable().DefaultView;
        Type thisType = businessEntity.GetType();
        while (thisType != typeof(Object))
        {
            PropertyInfo[] entityProperties = thisType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            for (int index = 0; index < dvSchema.Count; index++)
            {
                if (dr[index] != DBNull.Value)
                {
                    var property = from p in entityProperties
                                   where p.Name.ToUpper().Equals(dvSchema[index][0].ToString().ToUpper())
                                   select p;
                    if (property.Count() > 0)
                    {
                        object propertyNewValue;
                        if (property.First().PropertyType.BaseType != typeof(Enum))
                        {
                            propertyNewValue = dr[index];
                        }
                        else //enums throw a cast exception
                        {
                            if (dr[index].GetType() == typeof(string) || dr[index].GetType() == typeof(char))
                                propertyNewValue = Enum.Parse(property.First().PropertyType, ((int)dr[index].ToString()[0]).ToString());
                            else
                                propertyNewValue = Enum.Parse(property.First().PropertyType, dr[index].ToString());
                        }
                        if (property.First().CanWrite)
                        {
                            if (property.First().PropertyType.IsGenericType && property.First().PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                            {
                                Type[] typeCol = property.First().PropertyType.GetGenericArguments();
                                Type nullableType;
                                if (typeCol.Length > 0)
                                {
                                    nullableType = typeCol[0];
                                    if (nullableType.BaseType == typeof(Enum))
                                    {
                                        propertyNewValue = Enum.Parse(nullableType, propertyNewValue.ToString());
                                    }
                                }
                            }
                            property.First().SetValue(businessEntity, propertyNewValue, null);
                        }
                    }
                }
            }
            thisType = thisType.BaseType;
        }
    }

propertyNewValue的值为“091234”(如debbuger所见)

这段简单的代码运行良好:
var a=newtestclass();a、 GetType().GetProperty(“Test1”).SetValue(a,“000123312”,null);Assert(a.Test1==“000123312”)。所以你的应用程序中肯定有其他错误。例如,将字符串转换为int,然后再转换为string。@Nikolay请尝试将值“000123312”赋给对象变量,然后将该变量传递给SetValue。它删除了零吗?嗯,这里是
var a=newtestclass();对象测试=“000123312”;a、 GetType().GetProperty(“Test1”).SetValue(a,test,null);Assert(a.Test1==“000123312”)仍按预期工作。你能发布一些小代码来复制这个吗problem@Nikolay我添加了完整的过程代码(希望对您有所帮助,而不是混淆),在这段代码中,一切似乎都正常。您是否也可以显示业务实体属性的代码?你是怎么发现它会修零的?在UI、数据库中还是在业务实体属性中使用调试器?
property.First().SetValue(businessEntity, propertyNewValue, null);