标签: reflection

对于以 long 列表作为其唯一参数的方法,在 C# 中陷入反射状态

我的课堂上有这个方法TeacherBusiness.cs

public List<Teacher> GetList(List<long> ids)
Run Code Online (Sandbox Code Playgroud)

我想通过反射来调用它。这就是我所做的:

var ids = new List<long> { 1, 2, 3 }
var business = typeof(TeacherBusiness);
var getListMethod = business.GetMethod("GetList", new System.Type[] { typeof(List<long>) });
var entities = getListMethod.Invoke(business, new object[] { ids });
Run Code Online (Sandbox Code Playgroud)

但是,当我调用它时,我收到此错误:

对象与目标类型不匹配。

我被困在这一点上。

如果我将调用代码更改为getListMethod.Invoke(business, ids)代码将无法编译,并且出现此错误:

错误CS1503:参数2:无法从“System.Collections.Generic.List”转换为“对象?[]?”

我应该怎么办?

c# reflection

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

这可能在PHP?

请考虑以下PHP代码段:

<?php

class Is
{
    function __get($key)
    {
        $class = __CLASS__ . '_' . $key;

        if (class_exists($class) === true)
        {
            return $this->$key = new $class();
        }

        return false;
    }

    function Domain($string)
    {
        if (preg_match('~^[0-9a-z\-]{1,63}\.[a-z]{2,6}$~i', $string) > 0)
        {
            return true;
        }

        return false;
    }
}

class Is_Domain
{
    function Available($domain)
    {
        if (gethostbynamel($domain) !== false)
        {
            return true;
        }

        return false;
    }
}

$Is = new Is();

var_dump($Is->Domain('google.com')); // true
var_dump($Is->Domain->Available('google.com')); // false

?>
Run Code Online (Sandbox Code Playgroud)

是否可以像这样调用Available()方法(如果未调用Available方法,仍然只返回true或false)?

var_dump($Is->Domain('google.com')->Available()); // false
Run Code Online (Sandbox Code Playgroud)

如果有,怎么样?

编辑:这会成功吗? …

php oop reflection design-patterns chain-of-responsibility

-1
推荐指数
1
解决办法
215
查看次数

C#反射:获取调用类'Type

可能重复:
C#中的反射:如何获取调用方法名称和类型?

假设我有两个类:

public class Foo {
    public Foo() {
        Bar.Pirate();
    }
}

public static class Bar {
    public static void Pirate() {
        Type callingClassType = ...
    }
}
Run Code Online (Sandbox Code Playgroud)

在内部Pirate(),如何获取调用Type的class(Foo)Pirate()

c# reflection

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

GetValue通过反思

我希望通过循环遍历它们并使用如下反射来获取MDI表单值的每个ToolStripMenuItem:

FieldInfo[] menuitems = GetType().GetFields(BindingFlags.GetField | 
    BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var item in menuitems )
  if (item.FieldType.Equals(typeof(ToolStripMenuItem)))
      MessageBox.Show(
        item.FieldType.GetProperty("Tag").GetValue(item, null).ToString());        
Run Code Online (Sandbox Code Playgroud)

但我得到"对象不匹配目标类型"错误,我很困惑,不知道指定哪个对象作为获取值的源对象.

请指导我...提前谢谢你.

c# reflection

-1
推荐指数
1
解决办法
367
查看次数

python反射找到引用对象

如果我有以下代码

 class One:
   scope = 'first_scope'

 class Two:
   scope = 'second_scope'
   contained_object = One()
Run Code Online (Sandbox Code Playgroud)

我是否有可能 contained_object通过反思来确定它和引用它的对象是否具有相同的含义scope

谢谢

编辑:道歉,如果问题不清楚,我不太确定python术语如何问.我设计了一种样本

一个例子可能是

 def sample(input):
     #code in here to find out if input.scope
     # matches a.scope without having a reference to it

 a = Two()
 a.scope = 'first scope'
 a.contained_object.scope = 'will not match'
 sample(a.contained_object)
Run Code Online (Sandbox Code Playgroud)

python reflection

-1
推荐指数
1
解决办法
273
查看次数

从给定的类型创建 Expression&lt;Func&lt;T, object&gt;&gt;

我希望通过在代表给定类型的属性成员访问的代码中构建表达式来动态使用 CsvHelper。

我试图将这些表达式传递给的方法具有以下签名:

    public virtual CsvPropertyMap<TClass, TProperty> Map<TProperty>( Expression<Func<TClass, TProperty>> expression )
    {
        //
    }
Run Code Online (Sandbox Code Playgroud)

因此,对于要映射的任何给定类型,您通常会调用它,如下所示(对于具有名为“stringProperty”的属性的类型):

mapper.Map(x => x.StringProperty);
Run Code Online (Sandbox Code Playgroud)

传入一个 lambda 表达式,该 lambda 表达式在内部转换为 Expression<Func<T, object>>

我尝试使用表达式在代码中创建此表达式。在编译时它一切正常(因为它返回一个Expression<Func<TModel, object>>),但在运行时我得到一个异常“不是成员访问”。这是采用表示我要映射的属性的 PropertyInfo 对象的代码:

    private Expression<Func<TModel, object>> CreateGetterExpression( PropertyInfo propertyInfo )
    {
        var getter = propertyInfo.GetGetMethod();

        Expression<Func<TModel, object>> expression = m => getter.Invoke( m, new object[] { } );
        return expression;
    }
Run Code Online (Sandbox Code Playgroud)

基本上,如何在代码中正确构建该表达式?

c# reflection lambda expression

-1
推荐指数
1
解决办法
1341
查看次数

Java反射:如何获取评论?

我在 Java 解析方面遇到了一些困难。我需要以某种方式通过反射获取类、字段、方法等的注释。

我找到了JavaParser,它看起来可以引发注释,但我不知道如何做到这一点,因为所有示例都只是解析给定的字符串。我发现TypeSolver可以采用规范名称,但看起来它不能与注释一起使用。

我的问题是,如果我只有评论,Class<?>并且该项目还有其他一些jar's也应该反思的评论,那么如何找到评论。通过调试,我看到了原始源代码,看起来可以以某种方式完成。
谢谢。

PS我有源代码,我需要Class<?>与源代码匹配,然后通过JavaParser提取注释

java reflection javaparser

-1
推荐指数
1
解决办法
3959
查看次数

如何将类型值传递给类型为reflect.Type Golang的变量

我需要创建 StructField,其中需要传递 Type 字段的reflect.Type 值。我想将其他类型(如reflect.Bool、reflect.Int)传递给将在StructField 构造中使用的函数。我无法使用下面的代码执行此操作

reflect.StructField{
            Name: strings.Title(v.Name),
            Type: reflect.Type(reflect.String),
            Tag:  reflect.StructTag(fmt.Sprintf(`xml:"%v,attr"`, v.Name)),
        }
Run Code Online (Sandbox Code Playgroud)

因为它

Cannot convert an expression of the type 'Kind' to the type 'Type'
Run Code Online (Sandbox Code Playgroud)

我将如何实现它?

reflection types go

-1
推荐指数
1
解决办法
2031
查看次数

反射获取字段标签

在 go 中有一种使用反射来获取字段标签的好方法,只需将字段包装在反射库中的函数中即可。

我基本上是在尝试创建一个瘦数据访问对象,它允许在数据库中获取列名,而无需到处进行硬编码。

下面是带有 db 列名称作为标签的结构。

    // Table Structures
type CusipTableRow struct {
    Id          int64  `db:"id"`
    Cusip       string `db:"cusip"`
    Symbol      string `db:"symbol"`
    Active      int8   `db:"active"`
    Added_Time  int32  `db:"added_timestamp"`
    Description string `db:"description"`
    Exchange    string `db:"exchange"`
    AssetType   string `db:"asset_type"`
}
Run Code Online (Sandbox Code Playgroud)

除了下载另一个关于如何使用反射本质上调用这样的库以返回带有标签值的字符串之外的任何建议。

var row CusipTableRow
row.GetColumnName(row.Id) //Return column name based on tag.
Run Code Online (Sandbox Code Playgroud)

我正在考虑可能尝试使用 map[address] fieldTag,但由于没有完全掌握 unsafe 包而没有运气让它起作用。如果这种方法行得通,我想这样的电话可能会奏效:

row.GetColumnName(&row.Id) //Return column name based on tag.
Run Code Online (Sandbox Code Playgroud)

任何想法和建议都会很棒。

谢谢

reflection go

-1
推荐指数
1
解决办法
47
查看次数

无法将反射场投射到地图中

我试图将反射场投射到地图上,但它给了我这个错误:

java.lang.ClassCastException: java.lang.reflect.Field cannot be cast to java.util.Map
Run Code Online (Sandbox Code Playgroud)

我该如何解决?

最小可重复示例:


open class Base {
    var inherited: Map<String, String> = mapOf(Pair("testing", "testing"))
}

class Child: Base() {
    var notInherited: Int = 100
}


fun main() {
    val c = Child()
    var inheritedMap: Map<String, String> = c.javaClass.superclass.getDeclaredField("inherited") as Map<String, String> 
    println(inheritedMap)
}
Run Code Online (Sandbox Code Playgroud)

链接到代码游乐场:https://pl.kotl.in/xW8g4pNsX

java reflection kotlin

-1
推荐指数
1
解决办法
602
查看次数