标签: reflection

如何获取源文件名和类型成员的行号?

考虑到调试数据文件可用(PDB)并使用System.Reflection或其他类似框架(如Mono.Cecil),如何以编程方式检索源文件名和类型或类型成员的行号声明.

例如,假设您已将此文件编译为程序集:

C:\ MyProject的\ Foo.cs

1:    public class Foo
2:    {
3:       public string SayHello()
4:       {
5:           return "Hello";
6:       }
7:    }
Run Code Online (Sandbox Code Playgroud)

怎么做这样的事情:

MethodInfo methodInfo = typeof(Foo).GetMethod("SayHello");
string sourceFileName = methodInfo.GetSourceFile(); // ?? Does not exist!
int sourceLineNumber = methodInfo.GetLineNumber(); // ?? Does not exist!
Run Code Online (Sandbox Code Playgroud)

sourceFileName将包含"C:\ MyProject\Foo.cs",sourceLineNumber将等于3.

更新:System.Diagnostics.StackFrame确实能够获取该信息,但仅限于当前执行调用堆栈的范围.这意味着必须首先调用该方法.我想获得相同的信息,但不调用类型成员.

c# reflection

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

我应该使用什么而不是LoadWithPartialName()?

我正在使用LoadWithPartialName()加载程序集,但VS告诉我它已过时并使用Load()代替.但是,我找不到任何方便的过载.

有一个Load(字符串)请求"全名",如果我理解正确的MSDN文档,包括版本号之类的东西.

还有一个Load(string,Evidence)接受"显示名称".问题是我没有丝毫知道"显示名称"是什么,因为我使用第一个功能的"部分名称"似乎不起作用.

那么,应该怎么做呢?

c# reflection obsolete

18
推荐指数
2
解决办法
5866
查看次数

如何在运行时加载程序集并创建类实例?

我有一个集会.在这个程序集中,我有一个类和接口.我需要在运行时加载这个程序集,并希望创建该类的对象,并且还想使用该接口.

Assembly MyDALL = Assembly.Load("DALL"); // DALL is name of my dll
Type MyLoadClass = MyDALL.GetType("DALL.LoadClass"); // LoadClass is my class
object obj = Activator.CreateInstance(MyLoadClass);
Run Code Online (Sandbox Code Playgroud)

这是我的代码.怎么可以改进?

c# reflection dll load assemblies

18
推荐指数
2
解决办法
6万
查看次数

为什么反射可以在C#中访问类的受保护/私有成员?

为什么反射可以在C#中访问类的受保护/私有成员?

这对于全班来说是不安全的,为什么反思会给予这种力量?这是反模式吗?

c# reflection

18
推荐指数
2
解决办法
1816
查看次数

反射 - 检查所有可空属性是否具有值

我必须遍历几个类中的所有属性并检查任何可空属性以查看它们是否具有值.如何将propertyInfo.GetValue()返回的值转换为通用的可空类型,以便我可以检查HasValue属性?

代码剪裁简洁:

foreach (PropertyInfo propInfo in this.GetType().GetProperties())
{
    if (<Snip: Check to see that this is a nullable type>)                                                                      
    {
           //How do i cast this properly in here to allow me to do:
           if(!((Nullable)propInfo.GetValue(this, null)).HasValue)
                  //More code here
    }
}
Run Code Online (Sandbox Code Playgroud)

c# generics reflection nullable

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

哪些Scala功能是使用反射在内部实现的?

众所周知,结构类型是通过反射实现的.是否有任何其他语言结构使用反射?

reflection scala

18
推荐指数
4
解决办法
927
查看次数

用Dapper调用自定义构造函数?

我正在尝试使用Dapper与ASP.NET SQL Membership Provider表进行交互.我包装了SqlMembershipProvider类并添加了一个额外的方法来获取MembershipUsers给出一些与我的一些自定义表相关的标准.

使用Dapper查询数据时,似乎Dapper首先使用无参数构造函数实例化该类,然后将返回的列"映射"到对象的属性中.

但是,MembershipUser类上的UserName属性没有setter.从Dapper SqlMapper.cs中的第1417行开始,该方法GetSettableProps()只获得可设置的属性.

我试图做一个MultiMap查询来调用构造函数,但问题是传递给查询的对象已经缺少UserName.

我猜我可以修改GetSettableProps()方法,但我不确定这是否有效,或者它是否会影响我现有的代码.

反正我是否要调用MembershipUser类具有的自定义构造函数?

或者我是否可以对Dapper做出合理的改变以支持我的情况?

**更新**

Marc使用非泛型/动态Query()方法的答案是正确的,但对后人来说,这是我在Dapper中引用的方法:

static List<PropInfo> GetSettableProps(Type t)
{
    return t
          .GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
          .Select(p => new PropInfo
          {
              Name = p.Name,
              Setter = p.DeclaringType == t ? p.GetSetMethod(true) : p.DeclaringType.GetProperty(p.Name).GetSetMethod(true),
              Type = p.PropertyType
          })
          .Where(info => info.Setter != null)
          .ToList();  
}
Run Code Online (Sandbox Code Playgroud)

c# reflection asp.net-membership dapper

18
推荐指数
1
解决办法
7914
查看次数

在Golang中使用反射获取函数名称

我正在尝试使用Go的反射系统来检索函数的名称,但是在调用其类型的Name方法时我得到一个空字符串.这是预期的行为吗?

这是我如何处理问题的一个简单示例:

package main

import "fmt"
import "reflect"

func main() {
    typ := reflect.TypeOf(main)
    name := typ.Name()
    fmt.Println("Name of function" + name)
}
Run Code Online (Sandbox Code Playgroud)

reflection go go-reflect

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

调用PropertyInfo.GetValue时参数计数不匹配异常

我正在尝试使用反射在运行时比较两个对象,以使用以下方法循环其属性:

Private Sub CompareObjects(obj1 As Object, obj2 As Object)

    Dim objType1 As Type = obj1.GetType()

    Dim propertyInfo = objType1.GetProperties

    For Each prop As PropertyInfo In propertyInfo
        If prop.GetValue(obj1).Equals(prop.GetValue(obj2)) Then
            'Log difference here
        End If
    Next
End Sub
Run Code Online (Sandbox Code Playgroud)

每当我测试这个方法时,我会在调用prop.GetValue(obj1)时从System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck中获取参数计数不匹配异常.

无论obj1和obj2的类型,还是prop中的类型(在我的测试用例中,第一个属性是布尔值)都会发生这种情况.

我做错了什么以及如何修复它以便我可以比较两个对象的值?

解

我在for循环中添加了以下几行:

Dim paramInfo = prop.GetIndexParameters
If paramInfo.Count > 0 Then Continue For
Run Code Online (Sandbox Code Playgroud)

第一个属性是一个参数,这导致了问题.现在,我将跳过任何需要参数的属性.

.net reflection

18
推荐指数
2
解决办法
8856
查看次数

无法导入scala.reflect.runtime.universe

我想按照本教程中的示例来讨论scala中的反射(2.10.2) .当我启动sbt(版本0.13)并导入scala.refelct.runtime.universe._时工作正常

scala> import scala.reflect.runtime.universe._                                                                                                                                       ?~                                                                                                                                                                                   
import scala.reflect.runtime.universe._
Run Code Online (Sandbox Code Playgroud)

但是当我尝试将示例代码放到像这样的对象时

object ReflectExample {
   import scala.reflect.runtime.universe._
   /*
    the rest of the Example
   */
}
Run Code Online (Sandbox Code Playgroud)

并通过sbt compile编译代码我看到以下错误消息:

[error]  object runtime is not a member of package reflect                                                                                                                                                                                                                                                       
[error]     import scala.reflect.runtime.universe._
Run Code Online (Sandbox Code Playgroud)

reflection scala

18
推荐指数
2
解决办法
6473
查看次数