有没有办法判断一个方法是否是一个覆盖?例如
public class Foo
{
public virtual void DoSomething() {}
public virtual int GimmeIntPleez() { return 0; }
}
public class BabyFoo: Foo
{
public override int GimmeIntPleez() { return -1; }
}
Run Code Online (Sandbox Code Playgroud)
是否可以反思BabyFoo并判断是否GimmeIntPleez是覆盖?
我正在尝试在Metro Style App便携式库中的类上定义和检索自定义属性.
就像是
[AttributeUsage(AttributeTargets.Class)]
public class FooAttribute : Attribute
{
}
[Foo]
public class Bar
{
}
class Program
{
static void Main(string[] args)
{
var attrs = CustomAttributeExtensions.GetCustomAttribute<FooAttribute>(typeof(Bar));
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于普通的4.5,但在一个便携式图书馆中,它针对地铁风格的应用程序,它告诉我
Cannot convert type 'System.Type' to 'System.Reflection.MemberInfo'
Run Code Online (Sandbox Code Playgroud)
谢谢
c# custom-attributes system.reflection portable-class-library windows-runtime
我有两个组件: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) 我正在研究WPF应用程序,我只在运行时在单个设备上收到此错误.
Exception Info: System.Reflection.TargetInvocationException
Run Code Online (Sandbox Code Playgroud)
我的问题:
Exception Info: System.Reflection.TargetInvocationException
Stack:
at System.RuntimeMethodHandle.InvokeMethod(System.Object, System.Object[], System.Signature, Boolean)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(System.Object, System.Object[], System.Object[])
at System.Delegate.DynamicInvokeImpl(System.Object[])
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) …Run Code Online (Sandbox Code Playgroud) 我想动态生成程序集,它可以具有不同结构的函数.更准确地说,这些函数可以是递归的,它们可以调用同一程序System.Reflection集中的其他函数等.我找到了理论上提供工具的模块,但实际上我遇到了这种方法的许多缺点.例如 - 我不能通过TypeBuilder和MethodBuilder类生成递归函数,因为将抛出异常(使用不完整类型).我了解到我可以通过以下方式生成自递归函数IlGenerator- 但它太麻烦了 - 我希望有一种更简单的方法可以做到这一点.
这是我的程序,它演示了这个问题(在生成方法时Fact抛出以下异常:
Run Code Online (Sandbox Code Playgroud)Exception thrown: 'System.NotSupportedException' in mscorlib.dll Additional information: Specified method is not supported..
代码:
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Linq.Expressions;
namespace GenericFuncs
{
public class ProgramBuilder
{
public Type createMyProgram()
{
var assmName = new AssemblyName("DynamicAssemblyExample");
var assmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assmName, AssemblyBuilderAccess.RunAndSave);
var moduleBuilder = assmBuilder.DefineDynamicModule(assmName.Name, assmName.Name + ".dll");
var myProgramType = buildMyProgram(moduleBuilder, moduleBuilder.DefineType("MyProgram", TypeAttributes.Public));
assmBuilder.Save(assmName.Name + ".dll");
return myProgramType;
}
private …Run Code Online (Sandbox Code Playgroud) 我正在尝试以一种方式编写一种方法,以编程方式为Memcached创建一个基于方法名称和参数的密钥.所以,如果我有一个方法,
string GetName(int param1, int param2);
Run Code Online (Sandbox Code Playgroud)
它会返回:
string key = "GetName(1,2)";
Run Code Online (Sandbox Code Playgroud)
我知道您可以使用反射获取MethodBase,但是如何获取字符串中的参数值,而不是参数类型?
我很好奇是否有人可以描述如何通过绑定实例枚举可用的ADSI方法[ADSI]$instance.psbase.Invoke()?
研究已经出现"参考ADSI接口的文档".但我对这个答案并不是特别满意.
如果我实例化:
[ADSI]$lhost_group="WinNT://./Administrators,group"
Run Code Online (Sandbox Code Playgroud)
然后尝试:
@($lhost_group.psbase.Invoke("Members")) | foreach-object {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
Run Code Online (Sandbox Code Playgroud)
Powershell将返回组中包含的每个对象的outof GetProperty("Name").
如何枚举通过任何给定ADSI接口可用的所有可用方法和属性?
这从吉文·利维答案就是语法的另一个例子[ADSI]$_.GetTypes().InvokeMember()和[ADSI]$_.psbase.Invoke()使用.
我有一个需要实例化一个未知类的工厂类。它是这样做的:
public class Factory
{
public void SetFoo(Type f)
{
Activator.CreateInstance(f);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我希望该构造函数是内部的,但是将其标记为内部会给我一个 MissingMethodException,但构造函数在同一个程序集中。如果它设置为 public 它工作正常。
我已经尝试了这里提供的解决方案 如何通过反射创建具有内部构造函数的类的对象实例?
这意味着这样做:
Activator.CreateInstance(f,true)
Run Code Online (Sandbox Code Playgroud)
但没有成功...
我使用 NETStandard.Library (1.6.1) 和 System.Reflection (4.3.0)
更新:
本次更新时提供的答案为我指明了正确的方向:
var ctor = f.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic);
var instance = (f)ctor[0].Invoke(null);
Run Code Online (Sandbox Code Playgroud)
谢谢你们!
当我发现一个奇怪的案例时,我正在玩属性和反射.当我尝试获取自定义属性的构造函数参数时,下面的代码在运行时给了我一个异常.
using System;
using System.Reflection;
class Program
{
[Test(new[] { Test.Foo }, null)]
static void Main(string[] args)
{
var type = typeof(Program);
var method = type.GetMethod("Main", BindingFlags.Static | BindingFlags.NonPublic);
var attribute = method.GetCustomAttributesData()[0].ConstructorArguments;
Console.ReadKey();
}
}
public enum Test
{
Foo,
Bar
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class TestAttribute : Attribute
{
public TestAttribute(Test[] valuesOne, Test[] valuesTwo)
{
}
}
Run Code Online (Sandbox Code Playgroud)
问题似乎是传递给Test属性构造函数的参数.如果其中一个为null,则ConstructorArguments抛出异常.唯一的例外是ArgumentException与name作为异常消息.
这是来自ConstructorArguments调用的堆栈跟踪:
System.RuntimeTypeHandle.GetTypeByNameUsingCARules(String name, RuntimeModule scope)
System.Reflection.CustomAttributeTypedArgument.ResolveType(RuntimeModule scope, String …Run Code Online (Sandbox Code Playgroud) 我正在为EF Core项目创建一个通用存储库,以避免为所有模型编写CRUD.我遇到的主要障碍是没有加载导航属性,因为Core还不支持延迟加载,而泛型类显然无法为类特定属性定义.Include语句.
我正在尝试为我的Get方法执行类似的操作以动态包含所有属性:
public virtual T Get(Guid itemId, bool eager = false)
{
IQueryable<T> querySet = _context.Set<T>();
if (eager)
{
foreach (PropertyInfo p in typeof(T).GetProperties())
{
querySet = querySet.Include(p.Name);
}
}
return querySet.SingleOrDefault(i => i.EntityId == itemId);
}
Run Code Online (Sandbox Code Playgroud)
但是,当包含不是导航属性的属性时,它会引发错误.
我发现这个答案与EF 5相同,但它涉及EF核心中不存在的方法:
是否有可能在EF Core中完成同样的事情?
c# ×9
.net ×4
reflection ×4
constructor ×1
internal ×1
memcached ×1
oop ×1
powershell ×1
syntax ×1
wpf ×1