假设我有以下程序:
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 …
假设我有两个类:
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)
我怎么能通过反思或其他解决方案来做到这一点?
我有一个像这样的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)
我怎样才能做到这一点?这甚至可能吗?
谢谢!
请考虑以下代码:
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的最佳方法是什么?
我目前有一个功能:
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没有任何类型参数的情况下使用,然后枚举结果.这值得么?
我有一个存储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(如果我保持用户字段的顺序相同).
请考虑以下代码:
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来获得结果吗?
我试图找出一个变量是简单的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) 考虑一下这个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版本中指定我希望返回值是具体类型?或者调用静态方法是唯一的方法吗?
reflection ×10
c# ×8
.net ×6
casting ×1
delegates ×1
generics ×1
go ×1
json ×1
lambda ×1
memcpy ×1
performance ×1
system.type ×1
type-systems ×1
types ×1