标签: system.reflection

System.Reflection.Assembly的空引用异常

我开发了一个用于内部电子邮件报告的库.当我从另一个项目中使用该库时(通过添加引用).

它给出NullReferenceException了以下几行.

System.Reflection.Assembly.GetEntryAssembly().GetName().Name
Run Code Online (Sandbox Code Playgroud)

任何想法,为什么大会是空的?

c# system.reflection

14
推荐指数
2
解决办法
7354
查看次数

Dapper with Attributes mapping

我尝试使用列属性映射我的Id字段,但由于某种原因,这似乎不起作用,我无法弄清楚原因.我建立了一个测试项目来展示我正在尝试的东西.

首先,我得到了我的2个实体:

实体表1

using System.Data.Linq.Mapping;

namespace DapperTestProj
{
    public class Table1
    {
        [Column(Name = "Table1Id")]
        public int Id { get; set; }

        public string Column1 { get; set; }

        public string Column2 { get; set; }

        public Table2 Table2 { get; set; }

        public Table1()
        {
            Table2 = new Table2();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

和实体表2

using System.Data.Linq.Mapping;

namespace DapperTestProj
{
    public class Table2
    {
        [Column(Name = "Table2Id")]
        public int Id { get; set; }

        public string Column3 { get; set; } …
Run Code Online (Sandbox Code Playgroud)

c# system.reflection dapper

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

将List <object>转换为List <Type>,Type在运行时已知

我正在实现某种反序列化,并在下一个问题上挣扎:

我有List<object>System.Reflection.Field,这是FieldType可以List<string>,List<int>或者List<bool>,所以我需要从转换List<object>到该类型.

public static object ConvertList(List<object> value, Type type)
{
   //type may be List<int>, List<bool>, List<string>
}
Run Code Online (Sandbox Code Playgroud)

我可以单独编写每个案例,但应该有更好的方法使用反射.

c# system.reflection

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

是否在某处定义了C#名称空间分隔符(.)?

C#中的全名分隔符是句点字符(.).例如System.Console.Write.

这是在某个地方定义的Path.PathSeperator,还是在.NET反射类中进行了硬编码?

(例如Type.FullName,Type.Namespace + "." + Type.Name假设它不会改变?

.net c# system.reflection

13
推荐指数
2
解决办法
711
查看次数

为什么CLR允许改变盒装的不可变值类型?

我有一种情况,我有一个简单的,不可变的值类型:

public struct ImmutableStruct
{
    private readonly string _name;

    public ImmutableStruct( string name )
    {
        _name = name;
    }

    public string Name
    {
        get { return _name; }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我打开这个值类型的实例时,我通常会期望当我执行unbox时,我装箱的内容会相同.令我惊讶的是,事实并非如此.使用Reflection有人可以通过重新初始化其中包含的数据来轻松修改我的盒子的内存:

class Program
{
    static void Main( string[] args )
    {
        object a = new ImmutableStruct( Guid.NewGuid().ToString() );

        PrintBox( a );
        MutateTheBox( a );
        PrintBox( a );;
    }

    private static void PrintBox( object a )
    {
        Console.WriteLine( String.Format( "Whats in the box: {0} :: {1}", ((ImmutableStruct)a).Name, a.GetType() ) );
    } …
Run Code Online (Sandbox Code Playgroud)

.net c# clr boxing system.reflection

12
推荐指数
1
解决办法
518
查看次数

如何将PropertyInfo转换为属性表达式并使用它来调用泛型方法?

如何转换PropertyInfo为可用于调用StructuralTypeConfiguration<TStructuralType>.Ignore<TProperty>(Expression<Func<TStructuralType, TProperty>> propertyExpression)方法的属性表达式?

我试图用来Expression.Property()构造表达式,但是当我使用这个表达式作为propertyExpression参数时,我得到了以下错误:

The type arguments for method cannot be inferred from the usage. Try specifying the type arguments explicitly.

此错误可能是指TProperty类型参数,我不知道如何指定只有PropertyInfo.

我这样做是关于:使用Entity Framework的StructuralTypeConfiguration.Ignore()忽略所有属性,但指定set.

UPDATE

代码不起作用:

var propertyInfo = typeof(Foo).GetProperties()[0];
var expression = Expression.Default(typeof(Foo));
var expressionProperty = Expression.Property(expression, propertyInfo);
Ignore(expressionProperty);
Run Code Online (Sandbox Code Playgroud)

.net c# linq system.reflection

12
推荐指数
1
解决办法
9118
查看次数

C#反射:如果......其他?

我目前正面临运营商的新问题.使用以下代码,我想使输出与if ... else在C#中使用pair 时的输出相同.

var method = new DynamicMethod("dummy", null, Type.EmptyTypes);
var g = method.GetILGenerator();

g.Emit(OpCodes.Ldstr, "string");
g.Emit(OpCodes.Ldstr, "string");
g.Emit(OpCodes.Call, typeof(String).GetMethod("op_Equality", new Type[]{typeof(string), typeof(string)}));
g.Emit(OpCodes.Ldc_I4, 0);
g.Emit(OpCodes.Ceq);
g.Emit(OpCodes.Brtrue_S, );

var action = (Action)method.CreateDelegate(typeof(Action));
action();

Console.Read();
Run Code Online (Sandbox Code Playgroud)

我的问题是:

  1. 如何获取指令的地址以将其作为分支操作码的参数传递?
  2. 有什么区别BRBR_S,BrtrueBrtrue_S,BrfalseBrfalse_S和类似的说明?

谢谢.

c# reflection reflection.emit system.reflection memory-address

12
推荐指数
2
解决办法
3118
查看次数

通过Reflection获取当前的班级成员

假设这段代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace TestFunctionality
{
  class Program
  {
    static void Main(string[] args)
    {
      // System.Reflection.Assembly.GetExecutingAssembly().Location
      Assembly assembly = Assembly.LoadFrom("c:\\testclass.dll");
      AppDomain.CurrentDomain.Load(assembly.GetName());
      var alltypes = assembly.GetTypes();
      foreach (var t in alltypes)
      {
        Console.WriteLine(t.Name);
        var methods = t.GetMethods(/*BindingFlags.DeclaredOnly*/);
        foreach (var method in methods)
        Console.WriteLine(
        "\t--> "
        +(method.IsPublic? "public ":"")
        + (method.IsPrivate ? "private" : "") 
        + (method.IsStatic ? "static " : "") 
        + method.ReturnType + " " + method.Name);
      }
      Console.ReadKey();
    } …
Run Code Online (Sandbox Code Playgroud)

c# system.reflection .net-assembly

12
推荐指数
1
解决办法
1116
查看次数

TypeBuilder中缺少CreateType.如何移植这个?

尝试将应用程序从.net 4.5移植到.net核心以供客户端使用.我注意到CreateType不再是TypeBuilder的一部分.我已经搜索了多个新的反射库而没有运气.有谁知道如何移植这个?

有问题的代码:

typeBuilder.CreateType()
Run Code Online (Sandbox Code Playgroud)

c# system.reflection .net-core-rc2

11
推荐指数
1
解决办法
2452
查看次数

为什么我需要AssemblyResolve处理程序来处理已经加载的程序集?

我有两个组件:AppAddOn. App引用AddOn,但CopyLocal设置为false,因为AddOn将动态加载App.

这是以下代码AddOn:

namespace AddOn
{
    public class AddOnClass
    {
        public static void DoAddOnStuff()
        {
            Console.WriteLine("AddOn is doing stuff.");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这里的代码是App:

class Program
{
    static void Main(string[] args)
    {
        Assembly.LoadFrom(@"..\..\..\AddOn\bin\Debug\AddOn.dll");

        // Without this event handler, we get a FileNotFoundException.
        // AppDomain.CurrentDomain.AssemblyResolve += (sender, e) =>
        // {
        //     return AppDomain.CurrentDomain.GetAssemblies()
        //                     .FirstOrDefault(a => a.FullName == e.Name);
        //};

        CallAddOn();
    } …
Run Code Online (Sandbox Code Playgroud)

.net c# reflection system.reflection

10
推荐指数
1
解决办法
2021
查看次数