标签: reflection

加载同一程序集的不同版本

使用反射,我需要加载同一组件的2个不同版本.我可以在同一个进程中在2个不同的AppDomain中加载2个版本吗?

我需要从旧版本的应用程序到新版本进行一些数据迁移.如果可能,请告诉我,或者我应该使用2个单独的流程.

.net c# reflection dll assemblies

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

这个反射代码出了什么问题?GetFields()返回一个空数组

C#,Net 2.0

这是代码(我拿出了所有特定于域的东西,它仍然返回一个空数组):

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ChildClass cc = new ChildClass();
            cc.OtherProperty = 1;

            FieldInfo[] fi = cc.GetType().GetFields();
            Console.WriteLine(fi.Length);
            Console.ReadLine();
        }
    }
    class BaseClass<T>
    {
        private int myVar;

        public int MyProperty
        {
            get { return myVar; }
            set { myVar = value; }
        }


    }

    class ChildClass : BaseClass<ChildClass>
    {
        private int myVar;

        public int OtherProperty
        {
            get { return myVar; }
            set { …
Run Code Online (Sandbox Code Playgroud)

c# reflection type-parameter

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

如何从C#中的对象实例获取自定义属性

假设我有一个名为Test的类,其中一个名为Title的属性带有自定义属性:

public class Test
{
    [DatabaseField("title")]
    public string Title { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

还有一个名为DbField的扩展方法.我想知道是否可以在c#中从对象实例获取自定义属性.

Test t = new Test();
string fieldName = t.Title.DbField();
//fieldName will equal "title", the same name passed into the attribute above
Run Code Online (Sandbox Code Playgroud)

可以这样做吗?

.net c# reflection custom-attributes

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

如何使用Reflection为对象上的所有DateTime属性设置DateTime.Kind

在我的应用程序中,我通过Web服务检索域对象.在Web服务数据中,我知道所有日期值都是UTC,但Web服务不会将其xs:dateTime值格式化为UTC日期.(换句话说,该字母Z不会附加到每个日期的末尾以表示UTC.)

我无法在此时更改Web服务的行为方式,但作为一种解决方法,我创建了一个方法,在Web服务中的对象被反序列化后立即调用该方法.

    private void ExplicitlyMarkDateTimesAsUtc<T>(T obj) where T : class
    {
        Type t = obj.GetType();

        // Loop through the properties.
        PropertyInfo[] props = t.GetProperties();
        for (int i = 0; i < props.Length; i++)
        {
            PropertyInfo p = props[i];
            // If a property is DateTime or DateTime?, set DateTimeKind to DateTimeKind.Utc.
            if (p.PropertyType == typeof(DateTime))
            {
                DateTime date = (DateTime)p.GetValue(obj, null);
                date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
                p.SetValue(obj, date, null);
            }
            // Same check for nullable DateTime.
            else if …
Run Code Online (Sandbox Code Playgroud)

c# reflection datetime

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

通过反射检测可空类型

令人惊讶的是,以下代码未通过Assert:

int? wtf = 0;
Assert.IsType<Nullable<int>>(wtf);
Run Code Online (Sandbox Code Playgroud)

所以只是出于好奇,你怎么能确定给定的实例是否是Nullable <>对象?

c# reflection nullable

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

获取方法参数的名称

在Java 6中,想象一下我有以下方法签名:

public void makeSandwich(Bread slice1, Bread slice2, List<Filling> fillings, boolean mustard)
Run Code Online (Sandbox Code Playgroud)

我想知道,在运行时,传递给slice2或任何其他参数的值,这里重要的一点是我想通过参数名称获取值.

我知道如何用getParameterTypes或获取参数类型列表getGenericParameterTypes.

理想情况下,我想获得一个参数名称列表而不是类型.有办法吗?

java reflection runtime java-6

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

int.class在Java中是否等于Integer.class或Integer.TYPE?

让我们假设一个人检索Field使用反射的声明类型.

以下哪项测试将正确表明是否正在处理一个int或一个Integer?

Field f = ...
Class<?> c = f.getDeclaringClass();
boolean isInteger;

isInteger = c.equals(Integer.class);
isInteger = c.equals(Integer.TYPE);
isInteger = c.equals(int.class);

isInteger = ( c == Integer.class);
isInteger = ( c == Integer.TYPE);
isInteger = ( c == int.class);
Run Code Online (Sandbox Code Playgroud)

java reflection boxing primitive-types

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

避免在Type.GetType()中给出命名空间名称

Type.GetType("TheClass");
Run Code Online (Sandbox Code Playgroud)

null如果namespace不存在则返回如下:

Type.GetType("SomeNamespace.TheClass"); // returns a Type object 
Run Code Online (Sandbox Code Playgroud)

有没有办法避免给出这个namespace名字?

c# reflection types c#-4.0

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

c#中的函数装饰器

Python的函数装饰器是否有C#模拟?感觉它可以使用属性和反射框架,但我没有看到在运行时替换函数的方法.

Python装饰器通常以这种方式工作:

class decorator(obj):
    def __init__(self, f):
        self.f = f
    def __call__(self, *args, **kwargs):
        print "Before"
        self.f()
        print "After"

@decorator
def func1():
    print "Function 1"

@decorator
def func2():
    print "Function 2"
Run Code Online (Sandbox Code Playgroud)

然后调用func1和func2将导致

Before
Function 1
After
Before
Function 2
After

我们的想法是装饰器可以让我轻松地在多个功能的入口和出口点添加常见任务.

c# python reflection decorator

22
推荐指数
2
解决办法
5371
查看次数

使用Java反射仅获取类的公共方法

我正在尝试使用反射来获取在类中显式声明的所有公共方法(因此它c.getMethods()也不起作用,因为它也抓取了超类方法).我可以用

Method[] allMethods = c.getDeclaredMethods();
Run Code Online (Sandbox Code Playgroud)

从这个类中获取方法,但我只想使用公共方法.

此时,我正在尝试获取修改器并基于此执行某些操作,但由于某种原因,调试器中显示的修饰符值和修饰符值输出不同.例如,我有一个私有getNode方法,虽然"修饰符"值出现2在调试器中,但它会像"1"我一样输出System.out.println(c.getModifiers()).奇怪的.有另一种方法可以获得公共方法,还是我错过了一些明显的东西?谢谢你的帮助!

java reflection

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