我想将一个字符串转换为一个对象属性值,其名称我作为一个字符串.我试图这样做:
string modelProperty = "Some Property Name";
string value = "SomeValue";
var property = entity.GetType().GetProperty(modelProperty);
if (property != null) {
property.SetValue(entity,
Convert.ChangeType(value, property.PropertyType), null);
}
Run Code Online (Sandbox Code Playgroud)
问题是,当属性类型为可空类型时,这会失败并抛出Invalid Cast Exception.这不是无法转换的值的情况 - 如果我手动执行此操作它们将起作用(例如DateTime? d = Convert.ToDateTime(value);)我已经看到了一些类似的问题,但仍然无法使其工作.
在过去一年左右的时间里,我看到许多不同的价值转换器出于许多不同的目的,来自许多不同的作者.在我的脑海中突出的一件事是它们返回的"默认"值的广泛变化.例如;
public class MyConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// OK, we test for some undesirable, unconvertable situation, typically null...
if (value == null)
{
// And here are a variety of 'defaults' that I have seen, these begin the most typical.
return null;
return DependencyProperty.UnsetValue;
return Binding.DoNothing;
}
//...... other code.. whatever...
}}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,是否有一种"标准"方式来表明输入值无法转换?
如果我想要一个返回给定类型的默认值的方法,并且该方法是通用的,我可以返回一个默认值,如下所示:
public static T GetDefaultValue()
{
return default(T);
}
Run Code Online (Sandbox Code Playgroud)
如果我只将类型作为System.Type对象,我可以做类似的事情吗?
public static object GetDefaultValue(Type type)
{
//???
}
Run Code Online (Sandbox Code Playgroud) 我想实现一个简单的内存LRU缓存系统,我正在考虑一个基于IDictionary实现的解决方案,它可以处理散列LRU机制.来自java,我有经验LinkedHashMap,可以满足我的需要:我找不到任何类似的.NET解决方案.
有人开发过它或有没有人有这样的经历?
有了泛型,你可以
var object = default(T);
Run Code Online (Sandbox Code Playgroud)
但是当你拥有的只是一个Type实例时,我只能这样做
constructor = type.GetConstructor(Type.EmptyTypes);
var parameters = new object[0];
var obj = constructor.Invoke(parameters);
Run Code Online (Sandbox Code Playgroud)
甚至
var obj = type.GetConstructor(Type.EmptyTypes).Invoke(new object[0]);
Run Code Online (Sandbox Code Playgroud)
是不是有更短的方式,如泛型版本?
为什么这会返回False
public enum Directions { Up, Down, Left, Right }
static void Main(string[] args)
{
bool matches = IsOneOf(Directions.Right, Directions.Left, Directions.Right);
Console.WriteLine(matches);
Console.Read();
}
public static bool IsOneOf(Enum self, params Enum[] values)
{
foreach (var value in values)
if (self == value)
return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
这会返回True吗?
public static bool IsOneOf(Enum self, params Enum[] values)
{
foreach (var value in values)
if (self.Equals(value))
return true;
return false;
}
Run Code Online (Sandbox Code Playgroud) 在我之前的一个问题中,我询问如何使用 System.Text.Json填充现有对象。
一个很好的答案展示了一种解决方案,用 解析 json 字符串JsonDocument并用 枚举它EnumerateObject。
随着时间的推移,我的 json 字符串不断演变,现在还包含一个对象数组,当使用链接答案中的代码解析它时,它会抛出以下异常:
The requested operation requires an element of type 'Object', but the target element has type 'Array'.
Run Code Online (Sandbox Code Playgroud)
我发现人们可以以一种方式或另一种方式寻找JsonValueKind.Array, 并做这样的事情
if (json.ValueKind.Equals(JsonValueKind.Array))
{
foreach (var item in json.EnumerateArray())
{
foreach (var property in item.EnumerateObject())
{
await OverwriteProperty(???);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我无法做到这一点。
如何做到这一点并作为通用解决方案?
我想获得“结果 1”,其中添加/更新数组项,以及“结果 2”(当传递变量时),其中整个数组被替换。
对于“结果2”,我假设可以if (JsonValueKind.Array))在OverwriteProperty方法中检测到,以及在哪里/如何传递“replaceArray”变量?...在迭代数组或对象时?
一些示例数据:
Json 字符串首字母
{
"Title": "Startpage", …Run Code Online (Sandbox Code Playgroud) 什么是反射相当于:
default(object); //null
Run Code Online (Sandbox Code Playgroud)
当我在运行时之前没有类型时,例如
public void Method(Type type)
{
var instance = type.CreateDefault(); //no such method exists, but I expect there is a way of doing this?
}
Run Code Online (Sandbox Code Playgroud) 在c#中我可以default(T)用来获取类型的默认值.我需要在运行时从a获取默认类型System.Type.我怎样才能做到这一点?
例如某事(这不起作用)
var type = typeof(int);
var defaultValue = default(type);
Run Code Online (Sandbox Code Playgroud) 我正在使用现在的实体框架 - 但它是所有ORM甚至IEnumerable之间"共享"的问题.
假设我在MVC中有一个方法如下:
[HttpPost]
public ActionResult Foo(FooModel model)
{
var context = new Context(); -- The EF session
var data = context.Foo.Where(???).ToList();
return View(data);
}
Run Code Online (Sandbox Code Playgroud)
我想根据输入参数查询上下文,如:
var data = context.Foo.Where(x => x.Date == model.Date &&
x.Name == model.Name &&
x.ItemCode = model.ItemCode).ToList();
Run Code Online (Sandbox Code Playgroud)
但它比这更复杂,因为如果上面的一个参数(Date\ Name\ ItemCode)为null,我不想将它包含在查询中.
如果我硬编码它可能看起来像这样:
var query = context.Foo;
if (model.Date != null)
query =query.Where(x => x.Date == model.Date);
if (model.ItemCode != null)
query =query.Where(x => x.ItemCode == model.ItemCode);
...
Run Code Online (Sandbox Code Playgroud)
必须有一种比这更简单的方法.
我需要一种方法来生成Expression<T, bool> …
c# ×10
.net ×6
reflection ×4
generics ×2
asp.net-mvc ×1
boxing ×1
caching ×1
constructor ×1
default ×1
enums ×1
nhibernate ×1
wpf ×1