相关疑难解决方法(0)

如何遍历类的所有属性?

我上课了.

Public Class Foo
    Private _Name As String
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property

    Private _Age As String
    Public Property Age() As String
        Get
            Return _Age
        End Get
        Set(ByVal value As String)
            _Age = value
        End Set
    End Property

    Private _ContactNumber As String
    Public Property ContactNumber() As String
        Get
            Return _ContactNumber
        End Get
        Set(ByVal value As String)
            _ContactNumber = value
        End Set
    End Property


End …
Run Code Online (Sandbox Code Playgroud)

.net vb.net reflection properties class

165
推荐指数
4
解决办法
15万
查看次数

C#属性可以访问目标类吗?

我想通过使用反射从属性类访问类的属性.可能吗?

例如:

class MyAttribute : Attribute
{
    private void AccessTargetClass()
    {
        // Do some operations
    }
}

[MyAttribute]
class TargetClass
{
}
Run Code Online (Sandbox Code Playgroud)

c# reflection attributes

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

使用反射动态地将属性转换为其实际类型

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

解释我正在努力的真实场景.我试图在Entity Framework属性上调用"First"扩展方法.要在Framework上下文对象上调用的特定属性作为字符串传递给方法(以及要检索的记录的id).所以我需要对象的实际类型才能调用First方法.

我不能在对象上使用"Where"方法,因为lambda或delegate方法仍然需要对象的实际类型才能访问属性.

此外,由于对象是由实体框架生成的,因此我无法将类型转换为接口并对其进行操作.

这是场景代码:

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 …
Run Code Online (Sandbox Code Playgroud)

c# reflection entity-framework

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