如果我通过MetadataType属性将属性应用于部分类,则不会通过Attribute.IsDefined()找到这些属性. 任何人都知道为什么,或者我做错了什么?
下面是我为此创建的测试项目,但我真的尝试将自定义属性应用于LINQ to SQL实体类 - 就像这个问题中的答案一样.
谢谢!
using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
namespace MetaDataTest
{
class Program
{
static void Main(string[] args)
{
PropertyInfo[] properties = typeof(MyTestClass).GetProperties();
foreach (PropertyInfo propertyInfo in properties)
{
Console.WriteLine(Attribute.IsDefined(propertyInfo, typeof(MyAttribute)));
Console.WriteLine(propertyInfo.IsDefined(typeof(MyAttribute), true));
Console.WriteLine(propertyInfo.GetCustomAttributes(true).Length);
// Displays:
// False
// False
// 0
}
Console.ReadLine();
}
}
[MetadataType(typeof(MyMeta))]
public partial class MyTestClass
{
public string MyField { get; set; }
}
public class MyMeta
{
[MyAttribute()]
public string MyField …Run Code Online (Sandbox Code Playgroud) 我一直在搜索示例,但似乎找不到涉及提取某个文件夹的DotNetZip场景.我正在尝试从.zip文件中提取一个名为"CSS"的文件夹,它是.zip文件中的顶级文件夹.这是我到目前为止的代码:
using (ZipFile zip1 = ZipFile.Read(savedFileName))
{
var selection = from e in zip1.Entries
where System.IO.Path.GetFileName(e.FileName).StartsWith("CSS/")
select e;
foreach (var e in selection)
e.Extract(_contentFolder);
}
Run Code Online (Sandbox Code Playgroud)
当前的选择什么都没有,我可以使用一些帮助重写它,以便它提取css文件夹及其所有子目录和文件.
c# ×2
.net ×1
acronym ×1
attributes ×1
definition ×1
dotnetzip ×1
linq ×1
linq-to-sql ×1
metadata ×1