我开发了一个用于内部电子邮件报告的库.当我从另一个项目中使用该库时(通过添加引用).
它给出NullReferenceException了以下几行.
System.Reflection.Assembly.GetEntryAssembly().GetName().Name
Run Code Online (Sandbox Code Playgroud)
任何想法,为什么大会是空的?
我尝试使用列属性映射我的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) 我正在实现某种反序列化,并在下一个问题上挣扎:
我有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.Console.Write.
这是在某个地方定义的Path.PathSeperator,还是在.NET反射类中进行了硬编码?
(例如Type.FullName,Type.Namespace + "." + Type.Name假设它不会改变?
我有一种情况,我有一个简单的,不可变的值类型:
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) 如何转换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) 我目前正面临运营商的新问题.使用以下代码,我想使输出与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)
我的问题是:
BR和BR_S,Brtrue和Brtrue_S,Brfalse和Brfalse_S和类似的说明?谢谢.
c# reflection reflection.emit system.reflection memory-address
假设这段代码:
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) 尝试将应用程序从.net 4.5移植到.net核心以供客户端使用.我注意到CreateType不再是TypeBuilder的一部分.我已经搜索了多个新的反射库而没有运气.有谁知道如何移植这个?
有问题的代码:
typeBuilder.CreateType()
Run Code Online (Sandbox Code Playgroud) 我有两个组件:App和AddOn. 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)