我有一组假装付款的状态,每个都有付款ID.
我想获取每个付款ID的最新状态.测试我创建了一些虚拟数据然后尝试查询它.我到目前为止:
[Test]
public void GetPaymentLatestStatuses()
{
var client = new TestMongoClient();
var database = client.GetDatabase("payments");
var paymentRequestsCollection = database.GetCollection<BsonDocument>("paymentRequests");
var statusesCollection = database.GetCollection<BsonDocument>("statuses");
var payment = new BsonDocument { { "amount", RANDOM.Next(10) } };
paymentRequestsCollection.InsertOne(payment);
var paymentId = payment["_id"];
var receivedStatus = new BsonDocument
{
{ "payment", paymentId },
{ "code", "received" },
{ "date", DateTime.UtcNow }
};
var acceptedStatus = new BsonDocument
{
{ "payment", paymentId },
{ "code", "accepted" },
{ "date", DateTime.UtcNow.AddSeconds(-1) }
};
var completedStatus …Run Code Online (Sandbox Code Playgroud) c# mongodb mongodb-query aggregation-framework mongodb-.net-driver
我正在为属性构建自己的库,我想检查属性级别是否有属性.目前我有这种方法可以正常工作:
public static bool HasPropertyAttribute<T>(this object instance, string propertyName)
{
return Attribute.GetCustomAttributes(instance.GetType().GetProperty(propertyName), typeof(T), true).Any();
}
Run Code Online (Sandbox Code Playgroud)
现在我正在研究解决方案,它允许我传递lambda表达式而不是string作为propertyName.是否有一种优雅的方法来做到这一点,而不添加此方法成为两个通用类型依赖,aka:
HasPropertyAttribute<T, TProperty>(...).
Run Code Online (Sandbox Code Playgroud) 假设我有一个方法,它打印对象的某些属性的名称和值:
public void PrintProperties(object o, params string[] propertyNames)
{
foreach (var propertyName in propertyNames)
{
// get the property info,
// then get the property's value,
// print property-name and -value
}
}
// method can be used like this
PrintProperties(user, "FirstName", "LastName", "Email");
Run Code Online (Sandbox Code Playgroud)
现在,我不想将字符串列表传递给方法,而是想更改该方法,以便可以使用lambda表达式指定属性(不确定这是否是正确的术语).
例如,我希望能够以某种方式调用我的方法:
PrintProperties(user, u->u.FirstName, u->u.LastName, u->u.Email);
Run Code Online (Sandbox Code Playgroud)
目标是为用户提供智能感知支持,以防止输入错误.(类似于ASP.NET MVC帮助器方法,如TextBoxFor(u=>u.Name)).
我如何定义我的方法,然后如何PropertyInfo在方法中获取s?
我需要在MVC4剃须刀中验证我的输入.问题是,我想使用twitter bootstrap然后设置顶部的错误消息类,div如下所示:http://getbootstrap.com/css/#forms-control-states
我怎样才能改变父母div的课程?
razor unobtrusive-validation asp.net-mvc-4 twitter-bootstrap
我有一个案例,我将方法名称作为字符串传递给函数,但我不希望它硬编码.例如
void MyMethodName()
{
// some code
}
void SomeOtherMethod()
{
SomeExternalLibrary.ExternalClass.FunctionWithStringParameter("MyMethodName");
}
Run Code Online (Sandbox Code Playgroud)
我想要这样的东西:
FunctionWithStringParameter(MyMethodName.ToString());
Run Code Online (Sandbox Code Playgroud)
这样我可以通过"查找所有引用"来跟踪方法调用,并且我可以毫无后顾之忧地使用重构.
考虑以下通用抽象类的实现:
public abstract class BaseRequest<TGeneric> : BaseResponse where TRequest : IRequestFromResponse
{
public TGeneric Request { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
有没有机会获得属性的名称Request而没有从中继承的实例?
我需要Request字符串"Request"来避免使用硬编码字符串.任何想法如何通过反思来做到这一点?
我想编写一个扩展方法来获取 StringLength 属性上的 MaximumLength 属性的值。
例如,我有一个类:
public class Person
{
[StringLength(MaximumLength=1000)]
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我希望能够做到这一点:
Person person = new Person();
int maxLength = person.Name.GetMaxLength();
Run Code Online (Sandbox Code Playgroud)
使用某种反射可以实现吗?
我想编写获取全部或单个类属性名称的通用函数
public class MyClass
{
public int TemplateId { get; set; }
public int TemplateCategoryId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
示例:在其他方法中,我需要调用
var propertyName = MyClass.TemplateId.GetPrertyName(); // will Return TemplateId
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
c# ×7
reflection ×5
generics ×2
attributes ×1
lambda ×1
method-names ×1
mongodb ×1
razor ×1
types ×1