标签: system.reflection

快速访问在C#中保存属性的类型/方法/ ...

我在这里创建了一个名为AAtribute的自定义属性,例如一个名为B的类,其中一个或多个方法使用该属性.是否有可能获得保存属性(在本例中为BMethod1)的方法的MethodInfo作为(其中一个)属性而不遍历整个程序集并查看其属性的所有已定义方法?它们是其他AttributeTargets(参数/类型/属性/ ...)的模拟方式吗?我不想要使用这种类型的属性的所有方法的数组,而只需要使用此Attirbute对象的方法.我想用它来对方法添加额外的约束(返回类型,参数,名称,其他属性用法......).

[AttributeUsage(AttributeTargets.Method)]
public class AAtribute : Attribute {

    //some fields and properties

    public AAtribute () {//perhaps with some parameters
        //some operations
        MethodInfo mi;//acces to the MethodInfo with this Attribute
                      //as an Attribute (the question)
        //some operations with the MethodInfo
    }

    //some methods

}

public class B {

    //some fields, properties and constructors

    [A]
    public void BMethod1 () {
        //some operations
    }

    //other methods

}
Run Code Online (Sandbox Code Playgroud)

c# attributes system.reflection methodinfo

8
推荐指数
1
解决办法
1239
查看次数

PropertyInfo - 具有属性的GetProperties

我正在尝试为webform项目创建自定义属性验证.

我已经可以从我的类中获取所有属性,但现在我不知道如何过滤它们,只是获取具有某些属性的属性.

例如:

PropertyInfo[] fields = myClass.GetType().GetProperties();
Run Code Online (Sandbox Code Playgroud)

这将返回我所有的属性.但是,我怎么能使用像"testAttribute"这样的属性返回属性呢?

我已经搜索了这个,但经过几次尝试解决这个问题后,我决定在这里问一下.

c# system.reflection

8
推荐指数
2
解决办法
1万
查看次数

使用EF Core在通用存储库中使用Reflection包含所有导航属性

我正在为EF Core项目创建一个通用存储库,以避免为所有模型编写CRUD.我遇到的主要障碍是没有加载导航属性,因为Core还不支持延迟加载,而泛型类显然无法为类特定属性定义.Include语句.

我正在尝试为我的Get方法执行类似的操作以动态包含所有属性:

public virtual T Get(Guid itemId, bool eager = false)
        {
            IQueryable<T> querySet = _context.Set<T>();

            if (eager)
            {
                foreach (PropertyInfo p in typeof(T).GetProperties())
                {
                    querySet = querySet.Include(p.Name);
                } 
            }

            return querySet.SingleOrDefault(i => i.EntityId == itemId);
        }
Run Code Online (Sandbox Code Playgroud)

但是,当包含不是导航属性的属性时,它会引发错误.

我发现这个答案与EF 5相同,但它涉及EF核心中不存在的方法:

EF5如何获取域对象的导航属性列表

是否有可能在EF Core中完成同样的事情?

c# system.reflection entity-framework-core

8
推荐指数
1
解决办法
4031
查看次数

从.NET中的字符串获取Type对象的最佳方法

在.NET中将字符串转换为Type对象的最佳方法是什么?

需要考虑的问题:

  • 类型可以在不同的组件中.
  • 类型的程序集可能尚未加载.

这是我的尝试,但它没有解决第二个问题

Public Function FindType(ByVal name As String) As Type
    Dim base As Type

    base = Reflection.Assembly.GetEntryAssembly.GetType(name, False, True)
    If base IsNot Nothing Then Return base

    base = Reflection.Assembly.GetExecutingAssembly.GetType(name, False, True)
    If base IsNot Nothing Then Return base

    For Each assembly As Reflection.Assembly In _
      AppDomain.CurrentDomain.GetAssemblies
        base = assembly.GetType(name, False, True)
        If base IsNot Nothing Then Return base
    Next
    Return Nothing
End Function
Run Code Online (Sandbox Code Playgroud)

.net reflection system.reflection

7
推荐指数
2
解决办法
1万
查看次数

C#通过反射设置对象DateTime属性值

我想将我的对象的所有DateTime属性设置为默认日期.但是,如果我尝试通过反射设置值,我会得到异常:"对象与目标类型不匹配".

private void SetDefaultValues()
{
    DateTime dt = DateTime.Parse("1/1/2000", new CultureInfo("en-US", true));
    foreach (PropertyInfo p in this.GetType().GetProperties())
    {
        if (p.PropertyType.FullName == "System.DateTime")
        {                                      
            p.SetValue(dt, typeof(DateTime), null);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在做/思考一些根本不正确的事情吗?

.net c# reflection system.reflection c#-4.0

7
推荐指数
1
解决办法
3494
查看次数

为什么 Assembly.GetTypes() 需要引用?

我想从我的程序集中获取所有类型,但我没有引用,我也不关心它们。查找接口类型与引用有什么关系?我有办法解决这个问题吗?

Assembly assembly = Assembly.LoadFrom(myAssemblyPath);
Type[] typeArray = assembly.GetTypes();
Run Code Online (Sandbox Code Playgroud)

抛出: FileNotFoundException 无法加载文件或程序集“某些引用的程序集”或其依赖项之一。该系统找不到指定的文件。

c# reflection types system.reflection

7
推荐指数
1
解决办法
2910
查看次数

Winforms:获取发布版本号?

我有一个Winforms应用程序,并希望显示版本号,以便我们可以知道我们的更新脚本是否正确运行.有没有办法获取发布版本号(显示在应用程序的属性页,发布选项卡)?

当我使用Reflection.Assembly.GetExecutingAssembly().GetName().Version等时,似乎使用AssemblyInfo.vb中的AssemblyVersion编号,这是不一样的.

如果我在AssemblyInfo.vb中使用通配符,它​​会再次出现不同的数字.

vb.net system.reflection winforms

7
推荐指数
1
解决办法
2万
查看次数

为什么我在安装VS2012/.Net 4.5后在.Net 4.0 Assembly.GetCustomAttributes上得到ExecutionEngineException?

真的很简单.我已经将.Net 2-3.5的解决方案升级到.Net 4,它工作正常,我已经安装了VS 2012并使用它.Net 4.5我现在在尝试反映来自一个CustomAttributes时遇到ExecutionEngineException使用以下代码动态生成DLL:

Assembly assembly = Assembly.LoadWithPartialName("DavesNamespace.Custom");
var attributes = assembly.GetCustomAttributes(typeof(ChecksumAttribute), true);
Run Code Online (Sandbox Code Playgroud)

我得到了有或没有为GetCustomAttributes指定的Type的异常,但只是在那一点上它抛出异常,程序集成功加载,如果我坚持一个断点,我实际上可以查看CustomAttributes集合.

自安装vs2012以来,最初的.Net 2-3.5版本仍然有效.

任何帮助非常感谢,我发现一些问题源自vs2012以ExecutionEngineExceptions在线查看,但没有答案.这个问题可以通过删除VS2012和.Net 4.5来解决,但是因为我们打算在它发布时转移到VS2013公司,这不是真正的解决方案.

编辑:在示例应用程序中出现错误,即使应用程序是针对4.5构建的,仍然会抛出异常

堆栈跟踪在下面的评论中添加(它为空)

.net c# system.reflection .net-assembly visual-studio-2012

7
推荐指数
1
解决办法
1245
查看次数

typeof(DateTime?).Name == Nullable`1

在.Net中使用Reflection typeof(DateTime?).Name返回"Nullable`1".

有没有办法将实际类型作为字符串返回.(在本例中为"DateTime"或"System.DateTime")

我明白的DateTime?Nullable<DateTime>.除此之外,我只是在寻找可空类型的类型.

.net c# system.reflection

7
推荐指数
3
解决办法
7212
查看次数

生成 .NET 库公共 API 的文本文件,用于版本控制和兼容性跟踪

我维护了太多的 NuGet 包,并且我正在尝试找到一个工具来为每个程序集生成公共 API 表面的纯文本文件(如构建后步骤)。每个命名空间、类、接口、结构、方法签名、成员、字段都将是一行,全部按字母顺序排序。

每当我更改公共 API 界面时,文件都会发生src/PublicAPIs.txt更改,这将是令人惊奇的 - github diff 会立即向我显示我修改、删除或添加的内容,并且该文件对于跟踪 API 随着时间的推移发生的变化非常宝贵。

我认为,我不太可能意外暴露私有 API 或破坏现有 API。

我觉得这一定已经存在了,我只是错过了一些东西?我知道 Telerik JustAssembly 可以进行基本的 .dll 比较,但我正在寻找能够自动将文件写入 git 存储库的东西,这样我就不必记住打开工具,并且任何重大更改都会在我的过程中弹出。正常工作流程。

.net c# system.reflection

7
推荐指数
1
解决办法
740
查看次数