标签: reflection

代表行动<ref T1,T2>

我正在尝试创建一个带有ref参数的静态方法的委托.请不要问我为什么要做这样的蠢货.这是学习.Net,C#和反射工作以及如何优化它的全部内容.

我的代码是:

    public struct DataRow
    {

        private double t;
        static public void Cram_T(ref DataRow dr, double a_t)
        {
            dr.t = a_t;
        }
    }
 ''''
  Type myType = typeof(DataRow);
  MethodInfo my_Cram_T_Method = myType.GetMethod("Cram_T");
  var myCram_T_Delegate = 
         Delegate.CreateDelegate(typeof(Action<DataRow, Double>),      
                                 my_Cram_T_Method) 
                                 as Action<DataRow, Double>;
Run Code Online (Sandbox Code Playgroud)

这给了我一个绑定错误,因为(我认为)泛型操作与方法不匹配.

检查监视窗口中Cram_T_Method的值

{Void Cram_T(DataRow ByRef, Double)}
Run Code Online (Sandbox Code Playgroud)

然后我尝试在Action中使用ref关键字:

  var myCram_T_Delegate = 
         Delegate.CreateDelegate(typeof(Action<ref DataRow, Double>),         
                                 my_Cram_T_Method) 
                                 as Action<ref DataRow, Double>;
Run Code Online (Sandbox Code Playgroud)

但这不会编译.C#编译器在令牌"ref"处窒息.

创建此委托的正确方法是什么?

c# reflection delegates

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

C#反思:如何获取Nullable <int>的类型?

我想做的是这样的:

switch( myObject.GetType().GetProperty( "id") )
{
    case ??: 
        // when Nullable<Int32>, do this
    case ??:
        // when string, do this
    case ??:
        // when Nullable<bool>, do this
Run Code Online (Sandbox Code Playgroud)

object.GetType()下的什么路径将具有我可以使用case语句进行比较的数据类型的字符串名称?我需要知道类型,所以我可以使用许多Convert.ToInt32(字符串)中的一个,它将使用Reflection设置myObject的值.

.net c# reflection typeof gettype

29
推荐指数
3
解决办法
2万
查看次数

Java是否具有"是一种类"的测试方法

我有一个基类,Statement,这是其它几类从,继承命名IfStatement,WhereStatement等...什么是在执行测试的最佳方式if声明,以确定哪种类型的Statement一个实例从派生类?

java reflection evaluation inheritance

28
推荐指数
3
解决办法
4万
查看次数

我如何使用Activator.CreateInstance与字符串?

在我的反射代码中,我遇到了我的通用代码部分的问题.特别是当我使用一个字符串.

var oVal = (object)"Test";
var oType = oVal.GetType();
var sz = Activator.CreateInstance(oType, oVal);
Run Code Online (Sandbox Code Playgroud)

例外

An unhandled exception of type 'System.MissingMethodException' occurred in mscorlib.dll

Additional information: Constructor on type 'System.String' not found.
Run Code Online (Sandbox Code Playgroud)

我试过这个用于测试目的,它也发生在这个单一的衬里

var sz = Activator.CreateInstance("".GetType(), "Test");
Run Code Online (Sandbox Code Playgroud)

我最初写的

var sz = Activator.CreateInstance("".GetType());
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误

Additional information: No parameterless constructor defined for this object.
Run Code Online (Sandbox Code Playgroud)

如何使用反射创建字符串?

.net c# reflection activator

28
推荐指数
2
解决办法
3万
查看次数

如何获取具有命名空间和类名的方法名称?

我想获得完全限定的方法名称.我可以看到如何通过以下方式获取方法名称:

System.Reflection.MethodBase.GetCurrentMethod().Name
Run Code Online (Sandbox Code Playgroud)

但这只返回实际名称.我需要一切,比如:

My.Assembly.ClassName.MethodName
Run Code Online (Sandbox Code Playgroud)

c# reflection

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

Java反射:按声明顺序获取字段和方法

有没有办法按照声明的顺序使用反射获取类声明的字段(和方法)?根据该文件,方法和字段的顺序返回的getFields(),getDeclaredFields()等是不确定的.

使用Java反射中建议的注释可以指定像索引之类的东西:类字段和方法的顺序是否标准化?

有没有更好的选择,即不必手动指定索引?

现在在你问我需要什么之前:我们有一个方法,它将一个非常大的数据结构作为输入,并对其进行冗长的计算.为了创建单元测试,我们创建了一个方法,该方法接受输入对象和输出实例,并创建Java源代码(设置输入,调用计算方法,然后断言正确的结果)作为输出.当字段按声明顺序编写时,此代码更易读.

java reflection junit

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

如何使用反射获取类的所有静态属性及其值

我有一个这样的课:

public class tbl050701_1391_Fields
{
    public static readonly string StateName = "State Name";
    public static readonly string StateCode = "State Code";
    public static readonly string AreaName = "Area Name";
    public static readonly string AreaCode = "Area Code";
    public static readonly string Dore = "Period";
    public static readonly string Year = "Year";
}
Run Code Online (Sandbox Code Playgroud)

我想写一些返回Dictionary<string, string>具有这些值的语句:

Key                            Value
--------------------------------------------
"StateName"                    "State Name"
"StateCode"                    "State Code"
"AreaName"                     "Area Name"
"Dore"                         "Period"
"Year"                         "Year"
Run Code Online (Sandbox Code Playgroud)

我有这个代码来获取一个属性值:

public static string GetValueUsingReflection(object obj, string propertyName) …
Run Code Online (Sandbox Code Playgroud)

c# reflection

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

获取字段的名称

在Java中是否可以从实际字段中获取字符串中的字段名称?喜欢:

public class mod {
    @ItemID
    public static ItemLinkTool linkTool;

    public void xxx{
        String fieldsName = *getFieldsName(linkTool)*;
    }
}
Run Code Online (Sandbox Code Playgroud)

PS:我不是在寻找字段的类/类名或从字符串中获取字段.


编辑:当我看着它时,我可能不需要一个方法来获取字段的名称,Field实例(来自字段的"代码名称")就足够了.[例如Field myField = getField(linkTool)]

在Java本身可能没有我想要的东西.我将看看ASM库,但最后我可能最终使用字符串作为字段的标识符:/


编辑2:我的英语不是很好(但即使用我的母语,我也有解释这个问题),所以我再添加一个例子.希望现在会更清楚:

public class mod2 {
    @ItemID
    public static ItemLinkTool linkTool;

    @ItemID
    public static ItemLinkTool linkTool2;

    @ItemID
    public static ItemPipeWrench pipeWrench;

    public void constructItems() {
        // most trivial way
        linkTool = new ItemLinkTool(getId("linkTool"));
        linkTool2 = new ItemLinkTool(getId("linkTool2"));
        pipeWrench = new ItemPipeWrench(getId("pipeWrench"));

        // or when constructItem would directly write into field just
        constructItem("linkTool"); …
Run Code Online (Sandbox Code Playgroud)

java reflection field

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

Type.GenericTypeArguments属性与Type.GetGenericArguments()方法

Type.GenericTypeArguments属性和Type.GetGenericArguments()方法有什么区别?他们总是回归同样的事情,还是有不同的情况?

c# reflection f#

28
推荐指数
1
解决办法
5446
查看次数

检查Type或实例是否实现IEnumerable而不管Type T

我在我当前的项目中做了大量的反思,我正在尝试提供一些帮助方法,以保持一切整洁.

我想提供一对方法来确定类型或实例是否实现IEnumerable- 无论类型如何T.这就是我现在所拥有的:

public static bool IsEnumerable(this Type type)
{
    return (type is IEnumerable);
}

public static bool IsEnumerable(this object obj)
{
    return (obj as IEnumerable != null);
}
Run Code Online (Sandbox Code Playgroud)

当我用它们测试它们时

Debug.WriteLine("Type IEnumerable:   " + typeof(IEnumerable).IsEnumerable());
Debug.WriteLine("Type IEnumerable<>: " + typeof(IEnumerable<string>).IsEnumerable());
Debug.WriteLine("Type List:          " + typeof(List<string>).IsEnumerable());
Debug.WriteLine("Type string:        " + typeof(string).IsEnumerable());
Debug.WriteLine("Type DateTime:      " + typeof(DateTime).IsEnumerable());
Debug.WriteLine("Instance List:      " + new List<string>().IsEnumerable());
Debug.WriteLine("Instance string:    " + "".IsEnumerable());
Debug.WriteLine("Instance DateTime:  " + new DateTime().IsEnumerable());
Run Code Online (Sandbox Code Playgroud)

我得到了这个结果:

Type IEnumerable:   False …
Run Code Online (Sandbox Code Playgroud)

.net c# reflection inheritance types

28
推荐指数
3
解决办法
2万
查看次数

标签 统计

reflection ×10

c# ×7

.net ×3

java ×3

inheritance ×2

activator ×1

delegates ×1

evaluation ×1

f# ×1

field ×1

gettype ×1

junit ×1

typeof ×1

types ×1