小编fre*_*rik的帖子

从动态生成的类中获取属性

我遇到了一个问题,这个问题通常无法解决看似相同的问题.

考虑:

我有一组动态生成的类,继承自已知的基类(让我们调用它BaseClass).这些动态生成的类也具有动态生成Properties的关联属性.

属性也是自定义类,但不是动态生成的:

[AttributeUsage(AttributeTargets.Property)]
class TypeAttribute: Attribute
{
    private Type _type;
    public Type Type
    {
        get { return _type; }
    }

    public TypeAttribute(Type t)
    {
        _type = t;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我想,运行时当然是获取这个赋值属性的值:

List<PropertyInfo> result = target.GetType()
  .GetProperties()
  .Where(
     p =>
        p.GetCustomAttributes(typeof(TypeAttribute), true)
        //.Where(ca => ((TypeAttribute)ca).)
        .Any()
     )
  .ToList();
Run Code Online (Sandbox Code Playgroud)

where target是的子类BaseClass.result然而,清单是空的,这让我感到困惑.

我使用添加属性

PropertyBuilder propertyBuilder = tb.DefineProperty(propertyName, 
           PropertyAttributes.HasDefault, propertyType, null);
ConstructorInfo classCtorInfo = typeof(TypeAttribute).
           GetConstructor(new Type[] { typeof(Type) });
CustomAttributeBuilder myCABuilder = new …
Run Code Online (Sandbox Code Playgroud)

c# c#-4.0

5
推荐指数
1
解决办法
643
查看次数

将裸存储库上的 git 分支“master”重命名为“main”,无需在 GitHub 上搜索

我在服务器上有一个裸露的 git 存储库,其默认分支名为master. 我想把名字改成main.

当我搜索说明时,总是被告知在删除分支之前将defaultGitHub 上的分支更改为新分支。但我的仓库不在 GitHub 上。 当我尝试删除时,我收到此错误消息:mainmaster
master

$ git push origin --delete master
remote: error: By default, deleting the current branch is denied, because the next
remote: error: 'git clone' won't result in any file checked out, causing confusion.
remote: error: 
remote: error: You can set 'receive.denyDeleteCurrent' configuration variable to
remote: error: 'warn' or 'ignore' in the remote repository to allow deleting the
remote: error: current branch, with or …
Run Code Online (Sandbox Code Playgroud)

git

0
推荐指数
1
解决办法
1357
查看次数

标签 统计

c# ×1

c#-4.0 ×1

git ×1