我有一个类型,t我想获得具有该属性的公共属性列表MyAttribute.该属性标有AllowMultiple = false,如下所示:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
Run Code Online (Sandbox Code Playgroud)
目前我拥有的是这个,但我认为有更好的方法:
foreach (PropertyInfo prop in t.GetProperties())
{
object[] attributes = prop.GetCustomAttributes(typeof(MyAttribute), true);
if (attributes.Length == 1)
{
//Property with my custom attribute
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能改善这个?我很抱歉,如果这是重复的,那里有大量的反思线程......似乎这是一个非常热门的话题.
在线程什么是你最喜欢的"程序员无知"宠儿?,出现以下答案,有大量的赞成票:
Programmers who build XML using string concatenation.
我的问题是,为什么通过字符串连接(例如StringBuilder在C#中)构建XML 不好?
我过去已经多次这样做了,因为对于我正在使用的数据结构/对象来说,它有时是从A点到B点的最快捷方式.到目前为止,我提出了一些原因,为什么这不是最好的方法,但有什么我忽略的?为什么要避免这种情况?
<?xml version="1.0"?>).这有害吗?StringBuilder),这是否需要关注?据推测,类似的类XmlWriter也需要做一些字符串操作......XmlSerializer自动序列化/反序列化类.好的,我同意.C#有很多有用的类,但有时我不想为一些非常快速的类创建类,比如写出日志文件或其他东西.这只是我懒惰吗?如果我正在做一些"真实"的事情,这是我处理w/XML的首选方法.更新:如果这个方法不是线程安全的,这是可以接受的,但我有兴趣学习如何使它保持线程安全.另外,key如果我可以避免它,我不想锁定所有值的单个对象.
原始问题:假设我想编写一个带有键和函数的高阶函数,并检查对象是否已使用给定的密钥进行高速缓存.如果是,则返回缓存的值.否则,运行给定的函数并缓存并返回结果.
这是我的代码的简化版本:
public static T CheckCache<T>(string key, Func<T> fn, DateTime expires)
{
object cache = HttpContext.Current.Cache.Get(key);
//clearly not thread safe, two threads could both evaluate the below condition as true
//what can I lock on since the value of "key" may not be known at compile time?
if (cache == null)
{
T result = fn();
HttpContext.Current.Cache.Insert(key, result, null, expires, Cache.NoSlidingExpiration);
return result;
}
else
return (T)cache;
}
Run Code Online (Sandbox Code Playgroud)
另外,假设我key在编译时不知道所有可能的值.
如何使这个线程安全?我知道我需要在这里引入锁定,以防止1+线程将我的条件评估为真,但我不知道要锁定什么.我读过的许多关于锁定的例子(例如Jon Skeet的文章)建议使用仅用于锁定的"虚拟"私有变量.在这种情况下,这是不可能的,因为密钥在编译时是未知的.我知道我可以通过为每个人使用相同的锁来轻松地使这个线程安全 …
所以我很高兴从Eric Lippert那里读到这篇文章然后,当然,他们的优秀评论和John Payson说:
一个更有趣的例子可能是使用两个静态类,因为这样的程序可能会死锁而没有任何可见的阻塞语句.
我想,是的,这很容易,所以我把它打倒了:
public static class A
{
static A()
{
Console.WriteLine("A.ctor");
B.Initialize();
Console.WriteLine("A.ctor.end");
}
public static void Initialize()
{
Console.WriteLine("A.Initialize");
}
}
public static class B
{
static B()
{
Console.WriteLine("B.ctor");
A.Initialize();
Console.WriteLine("B.ctor.end");
}
public static void Initialize()
{
Console.WriteLine("B.Initialize");
}
public static void Go()
{
Console.WriteLine("Go");
}
}
Run Code Online (Sandbox Code Playgroud)
其输出(在调用之后B.Go())是:
B.ctor
A.ctor
B.Initialize
A.ctor.end
A.Initialize
B.ctor.end
Go
Run Code Online (Sandbox Code Playgroud)
没有僵局,我显然是一个失败者 - 所以为了使尴尬永久化,这是我的问题:为什么这里没有僵局?
这似乎是我的小的大脑,B.Initialize被称为前的静态构造函数B已完成,我认为这是不允许的.
我正在开始一个新项目,并在思考我应该记录什么.日志文件仅用于帮助开发人员发现错误.用例是当抛出未处理的异常时,会向开发人员发送通知,该开发人员可以访问日志文件和堆栈跟踪.
我应该在日志文件中包含哪些内容?记录一切都不会起作用.我知道这很难说,因为答案可能需要对系统有深入的了解.所以我想我真的要求"最佳实践".请举例说明.
它还取决于应用程序类型,如桌面客户端应用程序,桌面服务器或Web服务器?
我正在努力让布局看起来正确,我试图生成我问题的最短,最小可能的例子.
我的目标是在屏幕的顶部和底部有一个页眉和页脚视图,ListView在两者之间,使用另一个视图(让我们称之为标签,它是屏幕截图中的灰色框)直接位于屏幕下方ListView.ListView需要滚动时,应始终显示此标签和页脚.
当ListView不需要滚动时(这是正确的):

当ListView需要滚动时,页脚和灰色框被推离屏幕(错误):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="header"
android:padding="20dp"
android:textSize="18sp"
android:background="@color/red"/>
<ListView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/list" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this should be directly below the ListView, but not pushed off screen when the ListView needs to scroll"
android:padding="5dp"
android:background="@color/light_gray"
android:textColor="@color/black"/>
<!-- Used to push the footer to the bottom -->
<View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="footer" …Run Code Online (Sandbox Code Playgroud) 使用两个单独的调用来隐藏两个元素之间是否有任何性能差异,或者仅使用一个语句会更高/更低效?有什么实际区别吗?
$('#container1').hide();
$('#container2').hide();
Run Code Online (Sandbox Code Playgroud)
VS:
$('#container1, #container2').hide();
Run Code Online (Sandbox Code Playgroud)
另外,您认为哪个更具可读性?
我在基类中有一个用属性标记的属性,并且我想更改每个派生类中的一些属性。做这个的最好方式是什么?
据我所知,我必须在基类中将属性定义为抽象,并重写每个基类中的属性,并重新定义所有属性。这看起来确实多余,而且我对此并不着迷,因为我必须在每个派生类中重复公共属性。
这是我正在尝试做的事情的简化示例。我想MyAttribute在派生类中进行更改,但保持属性上的所有其他属性相同并在单个位置定义(即我不想XmlElement多次重新定义)。这可能吗?或者有更好的方法来做到这一点吗?或者我在这里完全滥用了属性?
using System;
using System.Xml;
using System.Xml.Serialization;
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class MyAttribute : System.Attribute
{
public MyAttribute() {}
public string A { get; set; }
public string B { get; set; }
}
public abstract class BaseClass
{
public BaseClass() {}
[XmlElement("some_property")]
[MyAttribute(A = "Value1", B = "Value2")]
public string SomeProperty { get; set; }
}
public class FirstDerivedClass : BaseClass
{
//I want to change value B to something else
//in …Run Code Online (Sandbox Code Playgroud) 假设查询中有5条记录,如何获得前1条记录?这是我目前的代码.
public Application GetByUserIdAndVersion(int userId, string version)
{
VettingDataContext dc = new VettingDataContext(_connString);
return (from a in dc.Applications
where a.UserId == userId && a.chr_Version == version
select a).SingleOrDefault<Application>();
}
Run Code Online (Sandbox Code Playgroud) c# ×5
.net ×3
android ×1
attributes ×1
inheritance ×1
jquery ×1
linq ×1
locking ×1
logging ×1
reflection ×1
string ×1
xml ×1