我正在尝试将一个简单的应用程序移植到Windows 8 Metro(WinRT).似乎缺少一些非常基本的方法.一个基本的例子:Type.GetProperty().它适用于Windows Phone 7,Silverlight和.NET客户端配置文件.我是否必须安装某些东西(例如,一个特殊的库),或者这种方法在.NET metro配置文件中根本不可用?
UPDATE
好的谢谢.现在我用this.GetType().GetTypeInfo().DeclaredProperties.
using System.Reflection;需要有这种GetTypeInfo()扩展方法.
我想在 Asp.Net Core 的操作中区分这两个 json 输入:
{
"field1": null,
"field2": null
}
Run Code Online (Sandbox Code Playgroud)
和
{
"field1": null,
}
Run Code Online (Sandbox Code Playgroud)
我在 C# 中有一个这样的普通类:
public class MyData
{
public string Field1 { get; set;}
public string Field2 { get; set;}
}
Run Code Online (Sandbox Code Playgroud)
我想运行一个可以接受 null 作为值的对象的部分更新,但是当该字段不在输入中时,这意味着我根本不想更新此字段(将其设置为 null 的其他内容) .
当使用多种语言处理动态对象时,有一种结构可让您获取属性的值,如果所述属性不存在,则返回默认值。
我想知道在.NET中使用动态时是否存在类似的方法/语法。我知道您可以将ExpandoObject强制转换为Dictionary,但是有时不能保证动态对象是Expando。
我在想一些与以下代码具有相同效果的东西
public class SomeClass
{
public string ValidProperty { get; set; }
}
dynamic t = new SomeClass() {
ValidProperty = "someValue"
};
Console.WriteLine(t.Get("ValidProperty", "doesn't exist")); // Prints 'someValue'
Console.WriteLine(t.Get("InvalidProperty", "doesn't exist")); // Prints 'doesn't exist'
Run Code Online (Sandbox Code Playgroud) 我想读取性能DirectoryEntry.不幸的是并非所有的记录都有employeeNumber财产,所以我需要检查它是否存在.我已经尝试过:
a == one DirectoryEntry record
a.GetType().GetProperty("employeeNumber")==null //always returns true
String.IsNullOrWhiteSpace(a.Properties["employeeNumber"].ToString()) //exception
Run Code Online (Sandbox Code Playgroud)
我还能尝试什么?
我在C#中有此代码
foreach (var entry in auditableTableEntries)
{
IAuditableTable table = (IAuditableTable)entry.Entity;
table.ModifiedBy = userId;
table.ModifiedDate = dateTime;
if (entry.State == EntityState.Added || entry.State == EntityState.Modified)
{
if (table.CreatedBy == null || table.CreatedBy == null)
{
table.CreatedBy = userId;
table.CreatedDate = dateTime;
}
}
}
Run Code Online (Sandbox Code Playgroud)
一些表对象具有属性modified,为此,我想将属性设置为秒数。自1970年以来。类似:
table.modified = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds
Run Code Online (Sandbox Code Playgroud)
但是,如何判断该表是否具有该属性?如果属性不存在,我不想设置该属性,因为我认为这会导致异常。
到目前为止,这是我尝试过的:
if (table.GetType().GetProperty("modified") != null)
{
// The following line will not work as it will report that
// an IAuditableTable does not have …Run Code Online (Sandbox Code Playgroud)