我想得到我的类的哪些属性具有一个具体字符串的确切属性.我有这个实现(属性和类):
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class IsDbMandatory : System.Attribute
{
public readonly string tableField;
public IsDbMandatory (string tableField)
{
this.tableField= TableField;
}
}
public Class MyClass
{
[IsDbMandatory("ID")]
public int MyID { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我以这种方式获得具有具体属性的属性:
public class MyService
{
public bool MyMethod(Type theType, string myAttributeValue)
{
PropertyInfo props =
theType.GetProperties().
Where(prop => Attribute.IsDefined(prop, typeof(IsDbMandatory)));
}
}
Run Code Online (Sandbox Code Playgroud)
但我只需要具有具体属性isDbMandatory和具体字符串的属性myAttributeValue.
我该怎么做 ?
有没有向模型添加自定义字段的简单方法?假设我有一个包含3个字段的表"user":id,name和surname.我要这个:
$user = User::model()->findByPk(1);
$echo $user->fullName; // echoes name and surname
Run Code Online (Sandbox Code Playgroud)
请注意:我希望通过sql添加此自定义字段,如同
$c = new CDbCriteria();
$c->select = 'CONCAT("user".name, "user".surname) as fullName';
$user = User::model()->find($c);
Run Code Online (Sandbox Code Playgroud)
问题是没有设置fullName属性.
UPD:
这是一个有点棘手问题的代码 - 来自另一个表的自定义字段.这就是它的完成方式:
$model = Application::model();
$model->getMetaData()->columns = array_merge($model->getMetaData()->columns, array('fullName' => 'CONCAT("u".name, "u".surname)'));
$c = new CDbCriteria();
$c->select = 'CONCAT("u".name, "u".surname) as fullName';
$c->join = ' left join "user" "u" on "t".responsible_manager_id = "u".id';
$model->getDbCriteria()->mergeWith($c);
foreach ($model->findAll() as $o) {
echo '<pre>';
print_r($o->fullName);
echo '</pre>';
}
Run Code Online (Sandbox Code Playgroud) 当我尝试从object函数返回时获取自定义属性null.为什么?
class Person
{
[ColumnName("first_name")]
string FirstName { get; set; }
Person()
{
FirstName = "not important";
var attrs = AttributeReader.Read(FirstName);
}
}
static class AttributeReader
{
static object[] Read(object column)
{
return column.GetType().GetCustomAttributes(typeof(ColumnNameAttribute), false);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在我的.net 4.5应用程序中实现基于声明的安全性.很多篮球跳过,但它基本上工作.
我不喜欢的唯一部分是我无法创建自己的属性.ClaimsPrincipalPermissionAttribute已被密封.为什么?
我总是在我的应用程序中标记,例如:
[ClaimsPrincipalPermission(SecurityAction.Demand, Resource = "Foo", Operation = "Bar")]
Run Code Online (Sandbox Code Playgroud)
因为我希望我的资源和操作字符串不会拼写错误并且易于重构,所以我创建了类,所以我可以这样做:
[ClaimsPrincipalPermission(SecurityAction.Demand, Resource = Resources.Foo, Operation = Operations.Foo.Bar)]
Run Code Online (Sandbox Code Playgroud)
(请注意,由于不同的资源可能具有不同的操作,因此操作本身由资源子类化.)
这一切都很好,花花公子,但每次输入或复制/粘贴都是一个很大的问题.我宁愿做类似的事情:
[DemandPermission(Resources.Foo, Operations.Foo.Bar)]
Run Code Online (Sandbox Code Playgroud)
我可以创建这个属性,但我需要从ClaimsPrincipalPermissionAttribute继承,我不能,因为它是密封的.:(
还有其他方法可以解决这个问题吗?也许我不需要继承,但我能以某种方式注册我自己的属性类型,所以它可以在所有相同的地方工作吗?
我今天发现一些令我困惑的事:
1.如果我有这个:
public interface INamed
{
[XmlAttribute]
string Name { get; set; }
}
public class Named : INamed
{
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
它提供以下输出(Name属性序列化为元素):
<Named>
<Name>Johan</Name>
</Named>
Run Code Online (Sandbox Code Playgroud)
如果我有这个:
public abstract class NamedBase
{
[XmlAttribute]
public abstract string Name { get; set; }
}
public class NamedDerived : NamedBase
{
public override string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
XmlSerializer抛出System.InvalidOperationException
成员'NamedDerived.Name'隐藏继承的成员'NamedBase.Name',但具有不同的自定义属性.
我用于序列化的代码:
[TestFixture]
public class XmlAttributeTest
{
[Test]
public void SerializeTest()
{
var named = new …Run Code Online (Sandbox Code Playgroud) 要使用正则表达式进行验证,我通常会这样做:
// In my ViewModel
[RegularExpression("MyRegex", ErrorMessageResourceName = "MyErrorMessage")]
public string MyField { get; set; }
Run Code Online (Sandbox Code Playgroud)
和HTML助手
@Html.TextBoxFor(model => model.MyField)
Run Code Online (Sandbox Code Playgroud)
生成一个如下所示的标记:
<input type="text" class="valid" name="MyField" value="" id="MyField" data-val="true" data-val-regex-pattern="MyRegex" data-val-regex="MyErrorMessage"></input>
Run Code Online (Sandbox Code Playgroud)
问题是我希望有一个动态数量的字段,现在正在使用
// In my ViewModel
[RegularExpression("MyRegex", ErrorMessageResourceName = "MyErrorMessage")]
public IList<string> MyField { get; set; }
Run Code Online (Sandbox Code Playgroud)
这次
@Html.TextBoxFor(model => model.MyField[0])
Run Code Online (Sandbox Code Playgroud)
将生成(没有正则表达式html属性)
<input id="MyField_0_" type="text" value="" name="MyField[0]"></input>
Run Code Online (Sandbox Code Playgroud)
如何data-val在我的ViewModel中绑定具有DataAnnotation验证属性的列表的元素时,如何确保创建html属性?
我正在寻找为系统中的某些重要函数调用发布自定义性能计数器.我想在生产环境中持续监控这些性能计数器.
有没有办法让我用自定义属性标记某些函数,该属性可以测量执行给定函数所花费的时间?我想避免注入自定义代码,从而用监控代码污染与业务相关的功能.
属性中的代码如何跟踪函数执行所花费的时间?
请不要建议使用Profiler.我不打算调试或基准性能.但只是想在全天候生产中跟踪它.
我有一个带有templatefield列的gridview.TemplateFields是这样的:
<asp:TemplateField HeaderText="Title" SortExpression="name" meta:resourcekey="BoundFieldResource1">
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server"
Text='<%# Bind("Name") %>'
meta:resourcekey="BoundFieldResource1"></asp:Label>
</ItemTemplate>
Run Code Online (Sandbox Code Playgroud)
我必须为此列的标题添加自定义属性,因此我删除了HeaderText并添加了以下内容:
<Headertemplate>
<asp:Label ID="lblTitleHeading" runat="server" Text="Title" data-custom="tbl-th_title_heading"></asp:Label>
</Headertemplate>
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我这样做时,它将打破该列的排序,我不能点击标题再对它进行排序,我尝试更改为但是没有做任何事情.我很感激你的回答.
sorting webforms custom-attributes templatefield aspxgridview
在我当前的项目styles.xml中,我尝试定义:
<resources xmlns:ripple="http://schemas.android.com/apk/res-auto">
<resources xmlns:ripple="http://schemas.android.com/apk/res/com.gorkem.components">
Run Code Online (Sandbox Code Playgroud)
我用这个:
<style name="FlatTextRipple">
<item name="ripple:rv_centered">true</item>
<item name="ripple:rv_color">#CCCCCC</item>
<item name="ripple:rv_type">simpleRipple</item>
<item name="ripple:rv_zoom">true</item>
<item name="ripple:rv_zoomDuration">300</item>
</style>
Run Code Online (Sandbox Code Playgroud)
在我的库项目中我有attrs.xml:
<declare-styleable name="RippleView">
<attr name="rv_alpha" format="integer" />
<attr name="rv_framerate" format="integer" />
<attr name="rv_rippleDuration" format="integer" />
<attr name="rv_zoomDuration" format="integer" />
<attr name="rv_color" format="color" />
<attr name="rv_centered" format="boolean" />
<attr name="rv_type" format="enum">
<enum name="simpleRipple" value="0" />
<enum name="doubleRipple" value="1" />
<enum name="rectangle" value="2" />
</attr>
<attr name="rv_ripplePadding" format="dimension" />
<attr name="rv_zoom" format="boolean" />
<attr name="rv_zoomScale" format="float" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
但我不明白Í什么时候使用它就像在布局中运行它但是当我使用style.xml gradle时不能attr它会给我这个错误:
Error:(6, 21) No …Run Code Online (Sandbox Code Playgroud) android custom-attributes android-custom-attributes android-gradle-plugin
我一直在寻找AOP日志记录的特定解决方案.我需要一个拦截,可以做这样的事情:
[MyCustomLogging("someParameter")]
Run Code Online (Sandbox Code Playgroud)
问题是,我在其他DI框架中看到了使这成为可能的例子.但我的项目已经在使用Autofac进行DI,我不知道与Unity混合是否是一个好主意(例如).在Autofac.extras.dynamiclibrary2中,InterceptAttribute类被密封.
任何人都有这个问题的想法?
Ps.:我对此感到满意:
[Intercept(typeof(MyLoggingClass), "anotherParameter"]
Run Code Online (Sandbox Code Playgroud) c# ×7
aop ×2
reflection ×2
.net-4.5 ×1
abstract ×1
android ×1
aspxgridview ×1
attributes ×1
autofac ×1
c#-4.0 ×1
interception ×1
interface ×1
sorting ×1
validation ×1
webforms ×1
wif ×1
yii ×1