标签: reflection

什么是RuntimeTypeHandle和Type之间的区别?

为什么我会在代码中使用一个而不是另一个?

.net c# reflection

16
推荐指数
2
解决办法
3005
查看次数

Type.GetMethods的BindingFlags,不包括属性访问器

假设我有以下程序:

namespace ReflectionTest
{
    public class Example
    {
        private string field;

        public void MethodOne() { }

        public void MethodTwo() { }

        public string Property
        {
            get { return field; }
            set { this.field = value; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            iterate(typeof(Example));

            Console.ReadLine();
        }

        public static void iterate(Type type)
        {
            MethodInfo[] methods = type.GetMethods(
                BindingFlags.DeclaredOnly |
                BindingFlags.Instance |
                BindingFlags.Public);

            foreach (MethodInfo mi in methods)
            {
                Console.WriteLine(mi.Name);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行程序时,我得到以下输出:

MethodOne
MethodTwo
get_Property
set_Property …

.net reflection

16
推荐指数
1
解决办法
4302
查看次数

如何在c#中使用相同的属性名将值从类X复制到类Y?

假设我有两个类:

public class Student
{
    public int Id {get; set;}
    public string Name {get; set;}
    public IList<Course> Courses{ get; set;}
}

public class StudentDTO
{
    public int Id {get; set;}
    public string Name {get; set;}
    public IList<CourseDTO> Courses{ get; set;}
}
Run Code Online (Sandbox Code Playgroud)

我想将Student类中的值复制到StudentDTO类:

var student = new Student();
StudentDTO studentDTO = student;
Run Code Online (Sandbox Code Playgroud)

我怎么能通过反思或其他解决方案来做到这一点?

.net c# reflection

16
推荐指数
3
解决办法
4998
查看次数

如何从C#中的字符串创建泛型类?

我有一个像这样的Generic类:

public class Repository<T> {...}
Run Code Online (Sandbox Code Playgroud)

我需要用字符串来实例...示例:

string _sample = "TypeRepository";
var _rep = new Repository<sample>();
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?这甚至可能吗?

谢谢!

.net c# generics reflection

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

检查System.Type是否是给定类的后代的最佳方法

请考虑以下代码:

public class A 
{
}  

public class B : A 
{
}  

public class C : B 
{
}  

class D  
{  
    public static bool IsDescendantOf(this System.Type thisType, System.Type thatType)  
    {  
        /// ??? 
    } 

    void Main()
    {
        A cValue = new C();
        C.GetType().IsDescendantOf(cValue.GetType());
    }
}
Run Code Online (Sandbox Code Playgroud)

实施IsDescendantOf的最佳方法是什么?

.net c# reflection type-systems system.type

16
推荐指数
3
解决办法
8113
查看次数

缓存或不缓存 - GetCustomAttributes

我目前有一个功能:

public static Attribute GetAttribute(MemberInfo Member, Type AttributeType)
{
    Object[] Attributes = Member.GetCustomAttributes(AttributeType, true);

    if (Attributes.Length > 0)
        return (Attribute)Attributes[0];
    else
        return null;
}
Run Code Online (Sandbox Code Playgroud)

我想知道是否值得将属性上的所有属性缓存到 Attribute = _cache[MemberInfo][Type] 字典中,

这将需要在GetCustomAttributes没有任何类型参数的情况下使用,然后枚举结果.这值得么?

c# reflection performance

16
推荐指数
3
解决办法
6087
查看次数

Go-复制结构体之间的所有常用字段

我有一个存储JSON的数据库,以及一个提供外部API的服务器,通过HTTP帖子,可以更改此数据库中的值.数据库由内部的不同进程使用,因此具有共同的命名方案.

客户看到的密钥是不同的,但是使用数据库中的密钥(有未公开的密钥)将1:1映射.例如:

这是在数据库中:

{ "bit_size": 8, "secret_key": false }
Run Code Online (Sandbox Code Playgroud)

这是呈现给客户:

{ "num_bits": 8 }
Run Code Online (Sandbox Code Playgroud)

API可以根据字段名称进行更改,但数据库始终具有一致的键.

我在结构中将字段命名为相同,并为json编码器指定了不同的标志:

type DB struct {
    NumBits int  `json:"bit_size"`
    Secret  bool `json:"secret_key"`
}
type User struct {
    NumBits int `json:"num_bits"`
}
Run Code Online (Sandbox Code Playgroud)

我正在使用encoding/jsonMarshal/Unmarshal.

这reflect是正确的工具吗?是否有更简单的方法,因为所有键都相同?我在想某种memcpy(如果我保持用户字段的顺序相同).

reflection json go memcpy

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

转换为C#中的反射类型

请考虑以下代码:

object objFoo = MakeFoo(); // object MakeFoo(){return new Foo();}
MethodInfo methodInfo = typeof(Program).GetMethod("Baz"); // Foo Baz(){return foo;}
Type typeFoo = methodInfo.ReturnType;
var result = (typeFoo)objFoo;
Run Code Online (Sandbox Code Playgroud)

我需要做一些魔法typeFoo来获得结果吗?

c# reflection types casting

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

正确区分布尔?和C#中的bool

我试图找出一个变量是简单的bool还是一个Nullable<bool>.

看起来

if(val is Nullable<bool>)
Run Code Online (Sandbox Code Playgroud)

两个返回true bool和Nullable<bool>变量

if(val is bool)
Run Code Online (Sandbox Code Playgroud)

也返回了两个真实的bool和Nullable<bool>.

基本上,我在发现如果一个简单有趣的bool变量是真 OR如果一个Nullable<bool>变量是不是空.

这样做的方法是什么?

这是完整的代码:

List<string> values = typeof(InstViewModel).GetProperties()
                          .Where(prop => prop != "SubCollection" && prop != "ID" && prop != "Name" && prop != "Level")
                          .Select(prop => prop.GetValue(ivm, null))
                          .Where(val => val != null && (val.GetType() != typeof(bool) || (bool)val == true))      //here I'm trying to check if val is …
Run Code Online (Sandbox Code Playgroud)

c# reflection

16
推荐指数
2
解决办法
1534
查看次数

委托返回类型与lambda函数不同

考虑一下这个MCVE:

using System;

public interface IThing { }

public class Foo : IThing
{
    public static Foo Create() => new Foo();
}

public class Bar : IThing
{
    public static Bar Create() => new Bar();
}

public delegate IThing ThingCreator();

class Program
{
    static void Test(ThingCreator creator)
    {
        Console.WriteLine(creator.Method.ReturnType);
    }

    static void Main()
    {
        Test(Foo.Create);      // Prints: Foo
        Test(Bar.Create);      // Prints: Bar

        Test(() => new Foo()); // Prints: IThing
        Test(() => new Bar()); // Prints: IThing
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么反映静态工厂方法的返回类型给出具体类型,而调用构造函数内联给出接口?我希望它们都是一样的.

另外,有没有办法在lambda版本中指定我希望返回值是具体类型?或者调用静态方法是唯一的方法吗?

.net c# reflection lambda delegates

16
推荐指数
2
解决办法
1083
查看次数