铸造属性,以动态地使用反射的实际类型 [英] Cast a property to its actual type dynamically using reflection

查看:104
本文介绍了铸造属性,以动态地使用反射的实际类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个属性动态转换为实际的类型。我如何/我可以做到这一点使用反射?

要解释一下,我上了一下工作的实际情况。我想呼吁实体框架性质的第一个扩展方法。上框架上下文对象被作为字符串传递给该方法被称为特定属性(以及该记录的ID来检索)。所以,我需要为了调用第一种方法的实际类型的对象。

我不能使用去哪儿方法的对象上的拉姆达或委托方法仍然需要以访问属性的实际类型的对象。

另外,作为对象由实体框架生成我不能施放的类型的接口,并在该操作

这是方案code:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用的System.Reflection;命名空间NmSpc
{    公共类ClassA的
    {
        公众诠释IntProperty {搞定;组; }
    }    公共类ClassB的
    {
        公共ClassA的myProperty的{搞定;组; }
    }    公共类ClassC
    {
        静态无效的主要(字串[] args)
        {
            ClassB的测试=新ClassB的();            的PropertyInfo propInfo = typeof运算(ClassB的).GetProperty(myProperty的);
            //得到一个类型的不安全参考ClassB`s财产
            对象属性= propInfo.GetValue(测试仪,NULL);            //获得类型安全的参考属性
            ClassA的typeSafeProperty =财产ClassA的;            //我需要的财产动态转换为实际的类型。我如何/我可以做到这一点使用反射?
            //我不知道,财产是ClassA的的除了在运行时
        }
    }
}


解决方案

我有一段时间了,所以我试图用VS2010解决我的问题,我想我是正确的previously时,我虽然是动态keywork将解决' 我的问题。请参阅下面的code。

 使用的System.Reflection;命名空间TempTest
{
    公共类ClassA的
    {
        公众诠释IntProperty {搞定;组; }
    }    公共类ClassB的
    {
        公共ClassB的()
        {
            myProperty的新= {的ClassA = IntProperty 4};
        }
        公共ClassA的myProperty的{搞定;组; }
    }    公共类节目
    {
        静态无效的主要(字串[] args)
        {
            ClassB的测试=新ClassB的();            的PropertyInfo propInfo = typeof运算(ClassB的).GetProperty(myProperty的);
            //得到一个类型的不安全参考ClassB`s财产
            动态属性= propInfo.GetValue(测试仪,NULL);            //铸造的财产,以它的实际类型动态
            INT结果= property.IntProperty;
        }
    }
}

I need to cast a property to its actual type dynamically. How do I/Can I do this using reflection?

To explain the real scenario that I am working on a bit. I am trying to call the "First" extension method on an Entity Framework property. The specific property to be called on the Framework context object is passed as a string to the method (as well as the id of the record to be retrieved). So I need the actual type of the object in order to call the First method.

I can't use the "Where" method on the object as the lambda or delegate method still needs the actual type of the object in order to access the properties.

Also as the object is generated by the Entity Framework I can't cast the type to an interface and operate on that.

This is scenario code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;

namespace NmSpc
{

    public class ClassA
    {
        public int IntProperty { get; set; }
    }

    public class ClassB
    {
        public ClassA MyProperty { get; set; }
    }

    public class ClassC
    {
        static void Main(string[] args)
        {
            ClassB tester = new ClassB();

            PropertyInfo propInfo = typeof(ClassB).GetProperty("MyProperty");
            //get a type unsafe reference to ClassB`s property
            Object property = propInfo.GetValue(tester, null);

            //get the type safe reference to the property
            ClassA typeSafeProperty = property as ClassA;

            //I need to cast the property to its actual type dynamically. How do I/Can I do this using reflection?
            //I will not know that "property" is of ClassA apart from at runtime
        }
    }
}

解决方案

I had some time so I tried to solve my problem using VS2010 and I think I was right previously when I though that the dynamic keywork would 'solve' my question. See the code below.

using System.Reflection;

namespace TempTest
{
    public class ClassA
    {
        public int IntProperty { get; set; }
    }

    public class ClassB
    {
        public ClassB()
        {
            MyProperty = new ClassA { IntProperty = 4 };
        }
        public ClassA MyProperty { get; set; }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            ClassB tester = new ClassB();

            PropertyInfo propInfo = typeof(ClassB).GetProperty("MyProperty");
            //get a type unsafe reference to ClassB`s property
            dynamic property = propInfo.GetValue(tester, null);

            //casted the property to its actual type dynamically
            int result = property.IntProperty; 
        }
    }
}

这篇关于铸造属性,以动态地使用反射的实际类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆