我正在寻找c#这样的机制:
Car car1;
Car car2;
Car car = (Car)SomeMechanism.Get("car1");
Run Code Online (Sandbox Code Playgroud)
car1和car2是领域
所以我想用反射得到一些对象,而不是键入:/我怎样才能在c#中做到?
假设通过编译代码字符串在内存中创建(可执行)程序集。然后我想将这个程序集对象序列化为一个字节数组,然后将其存储在数据库中。然后我想从数据库中检索字节数组并将字节数组反序列化回程序集对象,然后调用程序集的入口点。
起初,我只是尝试像处理 .net 中的任何其他简单对象一样执行此序列化,但显然这不适用于程序集对象。程序集对象包含一个名为 GetObjectData 的方法,该方法获取重新实例化程序集所需的序列化数据。所以我对如何将所有这些拼凑成我的场景有些困惑。
答案只需要展示如何获取程序集对象,将其转换为字节数组,将其转换回程序集,然后在反序列化的程序集上执行入口方法。
最重要的是,我知道AutoMapper,我不想使用它.因为我正在学习C#而我想深入了解它.所以我想尝试自己做这个问题(下面解释).
但是,我正在尝试创建一个属性复制器来处理一种类型属性的值到另一种属性,如果该属性具有相同的名称和类型,并且可以从源中读取并在目标中可写.我正在使用type.GetProperties()方法.采样方法如下:
static void Transfer(object source, object target) {
var sourceType = source.GetType();
var targetType = target.GetType();
var sourceProps = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
var targetProps = (from t in targetType.GetProperties()
where t.CanWrite
&& (t.GetSetMethod().Attributes & MethodAttributes.Static) == 0
select t).ToList();
foreach(var prop in sourceProps) {
var value = prop.GetValue(source, null);
var tProp = targetProps
.FirstOrDefault(p => p.Name == prop.Name &&
p.PropertyType.IsAssignableFrom(prop.PropertyType));
if(tProp != null)
tProp.SetValue(target, value, null);
}
}
Run Code Online (Sandbox Code Playgroud)
它的工作原理,但我在所以读一个答案,即采用System.Reflection.Emit和ILGenerator和 …
反正有没有检查方法是否使用PInvoke?我正在使用MethodBase循环遍历程序集中的所有方法,但我想检查该方法是否使用PInvoke.这是我正在使用的代码:
foreach (MethodBase bases in mtd.GetType().GetMethods())
{
//check if the method is using pinvoke
}
Run Code Online (Sandbox Code Playgroud)
另外,如果有可能,我怎么能检查正在使用的DLL和被调用的函数/入口点?
Activator.CreateInstance(p.PropertyType) == value 是假的
给定类型T:
public class ODataTestHelper
{
public int DataId { get; set; }
public string DataItem { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和物业的一个foreach:
public string BuildQuery<T>(T searchModel,int page, int pageSize)
{
var type=typeof(T);
var filters = new List<string>();
foreach (var p in type.GetProperties())
{
var value=p.GetValue(searchModel,null);
if(value == null || value.ToString().IsNullOrEmpty() || (p.PropertyType.IsValueType && Activator.CreateInstance(p.PropertyType) == value ))
continue;
filters.Add(string.Format("{0} eq {1}", p.Name, WrapTypeValue(p.PropertyType, value)));
}
if (filters.Any())
{
result += "&$filter=" + filters.Aggregate((s1, s2) => s1 + …Run Code Online (Sandbox Code Playgroud) 我遇到的情况是,我试图访问一个静态属性,该属性包含一个对象的单例,我希望仅通过知道其类型来检索该对象。我有一个实现,但看起来很麻烦......
public interface IFace
{
void Start()
}
public class Container
{
public IFace SelectedValue;
public Type SelectedType;
public void Start()
{
SelectedValue = (IFace)SelectedType.
GetProperty("Instance", BindingFlags.Static | BindingFlags.Public).
GetGetMethod().Invoke(null,null);
SelectedValue.Start();
}
}
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以做到以上几点?使用 System.Type 访问公共静态属性?
谢谢
我已经阅读了Evolving the Reflection API博客文章,并且当我在PCL中包含"旧"API时,我已经转换了大部分代码,但我无法找到IsInstanceOfType或等同于Type或TypeInfo.它是非常常用的,所以我怀疑它是在新的API下完全丢弃所以我打赌功能刚刚被重命名或折叠在一起其他功能,我只是没有看到它.
c# system.reflection base-class-library portable-class-library
我有一个包含一些ICollection类型属性的对象
所以基本上这个类看起来像这样:
Class Employee {
public ICollection<Address> Addresses {get;set;}
public ICollection<Performance> Performances {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
问题是通过使用反射获取Generic类内部的ICollection类型的属性名称.
我的通用类是
Class CRUD<TEntity> {
public object Get() {
var properties = typeof(TEntity).GetProperties().Where(m=m.GetType() == typeof(ICollection ) ...
}
Run Code Online (Sandbox Code Playgroud)
但它没有用.
我怎样才能在这里获得房产?
通常使用什么令牌?更重要的是,如果我有MetadataToken,是否可以取回MethodInfo对象?
结构中有一组预定义的颜色SkiaSharp.SKColors.它们作为静态字段公开公开SKColor.
我想提取这些字段并创建一个列表SKColor.我的尝试如下,但我不知道在代码中指出的地方该做什么.
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Example
{
class Program
{
static void Main()
{
Type type = typeof(SKColors);
FieldInfo[] fis = type.GetFields(BindingFlags.Static | BindingFlags.Public);
List<SKColor> colors = new List<SKColor>();
foreach(FieldInfo fi in fis)
{
//colors.Add(fi.WhatIsThis); // The point in question
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下是摘录SKColors:
//
// Just contains various utility colors
//
// Author:
// Miguel de Icaza
//
// Copyright 2016 Xamarin Inc
// …Run Code Online (Sandbox Code Playgroud) c# ×10
.net ×3
reflection ×2
.net-4.0 ×1
.net-core ×1
c#-4.0 ×1
ilgenerator ×1
mapping ×1
methodbase ×1
pinvoke ×1
skiasharp ×1
system.type ×1