我正在使用反射来遍历一个Type属性并将某些类型设置为默认值.现在,我可以对类型进行切换并default(Type)明确设置,但我宁愿在一行中进行.有默认的程序化等价物吗?
我得到了AutoMapperMappingException异常
抛出了"AutoMapper.AutoMapperMappingException"类型的异常.---> System.InvalidCastException:从'DummyTypes'到'System.Nullable`1 [[System.Int32,...]的无效转换
什么时候
public enum DummyTypes : int
{
Foo = 1,
Bar = 2
}
public class DummySource
{
public DummyTypes Dummy { get; set; }
}
public class DummyDestination
{
public int? Dummy { get; set; }
}
[TestMethod]
public void MapDummy()
{
Mapper.CreateMap<DummySource, DummyDestination>();
Mapper.AssertConfigurationIsValid();
DummySource src = new DummySource()
{
Dummy = DummyTypes.Bar
};
Mapper.Map<DummySource, DummyDestination>(src);
}
Run Code Online (Sandbox Code Playgroud)
AutoMapper不应该隐式地映射这个没有任何额外的显式规则吗?
PS我无法将DummyDestination.Dummy的定义更改为枚举.我必须处理这样的接口.
我在下面的方法中写了以下要求 -
如果传递的attributeName中没有值,则应返回 -
3.1.对于int -1 3.2.对于Datetime DateTime.MinValue 3.3.对于String,null 3.4.对于bool,null
情况3.4下面的方法失败.
public T AttributeValue<T>(XmlNode node, string attributeName)
{
var value = new object();
if (node.Attributes[attributeName] != null && !string.IsNullOrEmpty(node.Attributes[attributeName].Value))
{
value = node.Attributes[attributeName].Value;
}
else
{
if (typeof(T) == typeof(int))
value = -1;
else if (typeof(T) == typeof(DateTime))
value = DateTime.MinValue;
else if (typeof(T) == typeof(string))
value = null;
else if (typeof(T) == typeof(bool))
value = null;
}
return (T)Convert.ChangeType(value, typeof(T));
}
Run Code Online (Sandbox Code Playgroud)
当改变这个
public System.Nullable<T> AttributeValue<T>(XmlNode node, string …Run Code Online (Sandbox Code Playgroud) 我创建了一个扩展函数,它将字符串作为输入并检查值并基于Generic Type,将其转换为目标类型并返回它,它运行良好.
现在的问题是,如果我将输入值作为空传递它应该返回null,对于可空类型,但它只是抛出异常.
例如:如果我想将它转换为日期时间,它会抛出异常:
String was not recognized as a valid DateTime
Run Code Online (Sandbox Code Playgroud)
以下是我的代码:
public static class Extension
{
public static T ToNull<T>(this string value)
{
var stringType = "System.Nullable`1[[System.{0}, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]";
if (typeof(T) == typeof(String))
stringType = string.Format(stringType, "String");
if (typeof(T) == typeof(Int32?) || typeof(T) == typeof(Int32))
stringType = string.Format(stringType, "Int32");
if (typeof(T) == typeof(DateTime?) || typeof(T) == typeof(DateTime))
stringType = string.Format(stringType, "DateTime");
if (typeof(T) == typeof(Int64?) || typeof(T) == typeof(Int64))
stringType = string.Format(stringType, "Int64");
Type originalType = …Run Code Online (Sandbox Code Playgroud) c# asp.net generics extension-methods nullreferenceexception
这是我的代码。引发异常“类型'MyEnum.CommonCoreStandard'的对象不能转换为类型'System.Nullable`1 [System.Byte]'”。同时设置第二个属性“ Prop2”的值。我正在使用反射的概念将一种类型的“ BlEntity”转换为另一种类型的“ UiEntity”
public class BlEntity
{
//CommonCoreStandard is an enum
public CommonCoreStandard Prop1 { get; set; }
public CommonCoreStandard? Prop2 { get; set; }
}
public class UiEntity
{
public byte Prop1 { get; set; }
public byte? Prop2 { get; set; }
}
public void ConvertBlToUi()
{
var source = new BlEntity(CommonCoreStandard.KeyIdeasAndDetails, CommonCoreStandard.IntegrationOfKnowledge);
var target = new UiEntity();
TypeConverter.ConvertBlToUi(source,target);
}
public static void ConvertBlToUi<TBl, TUi>(TBl entitySource, TUi entityTarget)
{
var blProperties = typeof(TBl).GetProperties().Select(p => new { …Run Code Online (Sandbox Code Playgroud) 我有一个要从HTTP帖子中的JSON字符串反序列化的JObject,它表示一个自定义对象Adjuster。
然后,我将该对象与数据库中已存在的对象合并以创建更新的对象。我通过反射遍历属性并在属性名称匹配的地方分配它们来做到这一点。
我的问题是无法将JValue隐式转换为目标类型,我必须手动将其转换。有没有一种方法可以动态转换对象?我可以得到将需要转换为的类型。
这是我的模型活页夹:
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
JObject jsonObj = JsonConvert.DeserializeObject(actionContext.Request.Content.ReadAsStringAsync().Result) as JObject;
Adjuster dbAdjuster = new Adjuster();
Type adjType = dbAdjuster.GetType();
PropertyInfo[] props = adjType.GetProperties();
dbAdjuster = AdjusterFactory.GetAdjuster(Convert.ToInt32(jsonObj["ID"].ToString()));
foreach (var prop in jsonObj.Properties() )
{
foreach (PropertyInfo info in props)
{
if (prop.Name == info.Name && prop.Name != "ID")
{
if (info.GetValue(dbAdjuster) is string)
{
info.SetValue(dbAdjuster, Convert.ToString(prop.Value));
break;
}
//continue for every type
}
else
{
break;
}
}
}
bindingContext.Model = dbAdjuster; …Run Code Online (Sandbox Code Playgroud)