(参见下面我使用我接受的答案创建的解决方案)
我正在尝试提高一些涉及反射的代码的可维护性.该应用程序有一个.NET Remoting接口,公开(除此之外)一个名为Execute的方法,用于访问未包含在其已发布的远程接口中的应用程序部分.
以下是应用程序如何指定可通过Execute访问的属性(本例中为静态属性):
RemoteMgr.ExposeProperty("SomeSecret", typeof(SomeClass), "SomeProperty");
Run Code Online (Sandbox Code Playgroud)
所以远程用户可以调用:
string response = remoteObject.Execute("SomeSecret");
Run Code Online (Sandbox Code Playgroud)
并且应用程序将使用反射来查找SomeClass.SomeProperty并将其值作为字符串返回.
不幸的是,如果有人重命名SomeProperty并忘记更改ExposeProperty()的第3个parm,它会破坏这种机制.
我需要相当于:
SomeClass.SomeProperty.GetTheNameOfThisPropertyAsAString()
Run Code Online (Sandbox Code Playgroud)
在ExposeProperty中用作第三个parm,因此重构工具将负责重命名.
有没有办法做到这一点?提前致谢.
好的,这是我最终创建的内容(根据我选择的答案和他引用的问题):
// <summary>
// Get the name of a static or instance property from a property access lambda.
// </summary>
// <typeparam name="T">Type of the property</typeparam>
// <param name="propertyLambda">lambda expression of the form: '() => Class.Property' or '() => object.Property'</param>
// <returns>The name of the property</returns>
public string GetPropertyName<T>(Expression<Func<T>> propertyLambda)
{
var me = propertyLambda.Body as MemberExpression;
if (me == null)
{ …Run Code Online (Sandbox Code Playgroud) 我创建了将属性lambda转换为委托的方法:
public static Delegate MakeGetter<T>(Expression<Func<T>> propertyLambda)
{
var result = Expression.Lambda(propertyLambda.Body).Compile();
return result;
}
public static Delegate MakeSetter<T>(Expression<Action<T>> propertyLambda)
{
var result = Expression.Lambda(propertyLambda.Body).Compile();
return result;
}
Run Code Online (Sandbox Code Playgroud)
这些工作:
Delegate getter = MakeGetter(() => SomeClass.SomeProperty);
object o = getter.DynamicInvoke();
Delegate getter = MakeGetter(() => someObject.SomeProperty);
object o = getter.DynamicInvoke();
Run Code Online (Sandbox Code Playgroud)
但这些不会编译:
Delegate setter = MakeSetter(() => SomeClass.SomeProperty);
setter.DynamicInvoke(new object[]{propValue});
Delegate setter = MakeSetter(() => someObject.SomeProperty);
setter.DynamicInvoke(new object[]{propValue});
Run Code Online (Sandbox Code Playgroud)
MakeSetter行失败,"无法根据用法推断出类型参数.请尝试明确指定类型参数."
我正在尝试做什么?提前致谢.
我有一个Python脚本,可以处理来自.NET Remoting的异步回调.这些回调在虚拟(工作)线程中执行.从我的回调处理程序里面,我需要调用我在脚本中定义的函数,但是我需要在主线程中执行该函数.
主线程是一个向服务器发送命令的远程客户端.其中一些命令导致异步回调.
基本上,我需要相当于.NET的Invoke方法.这可能吗?
如何保护我的C#应用程序免受通过taskman或以编程方式杀死其进程的人员的攻击?
这是我的场景:
App A是由另一个团队开发的MFC应用程序.它有一个未发布的基于文本的远程接口,通过后门启用.
我正在开发应用程序B,一个与A交互的C#WinForms应用程序.当需要远程访问时,A启用A后门,在完成时(或失败时)关闭它.
我正在探索用户可以滥用B的方式来获取A的隐藏功能,例如在启用A的远程接口后杀死B的进程.当发生这种情况时,我希望B最后有机会关闭A的后门.
B使用localhost与A进行交互,所以我并不担心掉电情况.
我正在寻找一个不涉及改变A的解决方案.
我不希望能够阻止Dark Tangent(虽然这会是一个奖励),但是现在一个脚本小子可以用这个设计:)
这些应用程序在Windows XP上运行,但很快也将支持Vista和7.
吉姆,提前谢谢
在运行时,我希望能够卸载DLL并重新加载它的修改版本.我的第一个实验陷入了火焰之中.谁能告诉我为什么?
private static void Main()
{
const string fullPath = "C:\\Projects\\AppDomains\\distrib\\MyLibrary.dll";
// Starting out with a version of MyLibrary.dll which only has 1 method, named Foo()
AssemblyName assemblyName = AssemblyName.GetAssemblyName(fullPath);
AppDomain appDomain = AppDomain.CreateDomain("MyTemp");
appDomain.Load(assemblyName);
appDomain.DomainUnload += appDomain_DomainUnload;
AppDomain.Unload(appDomain);
// Breakpoint here; swap out different version of MyLibrary.dll which only has 1 method, named Goo()
AssemblyName assemblyName2 = AssemblyName.GetAssemblyName(fullPath);
AppDomain appDomain2 = AppDomain.CreateDomain("MyTemp2");
Assembly asm2 = appDomain2.Load(assemblyName2);
foreach (Type type in asm2.GetExportedTypes())
{
foreach (MemberInfo memberInfo in type.GetMembers())
{
string …Run Code Online (Sandbox Code Playgroud) 在日本的Windows PC上,这行C#抛出了一个格式异常:
double d = double.Parse("NaN");
Run Code Online (Sandbox Code Playgroud)
这条线在我的美国电脑上运行良好
不知道从哪里开始排除故障.有什么想法吗?
吉姆,提前谢谢
我有一个 C++/CLI DLL,我计划用作我的 C# DLL 和本机 C++ 客户端之间的适配器。我需要在两个方向传递字符串。该适配器是用 VS2013 编译的,但需要支持用 VS2008 构建的客户端,所以我在 API 中使用了 const char*。但是即使两者都是 VS2013 构建的,我所拥有的也不起作用。
在其他地方,我找到了使用 msclr\marshal.h 的建议,所以我创建了:
using namespace msclr::interop;
System::String^ ToCliString(const char* s)
{
System::String ^result = marshal_as<System::String^>(s);
return result;
}
const char* ToCppString(System::String^ s)
{
msclr::interop::marshal_context context;
const char* result = context.marshal_as<const char*>(s);
return result;
}
Run Code Online (Sandbox Code Playgroud)
为了测试这些,我在我的 C++/CLI DLL 中创建了一个往返转换方法:
const char* Foo(const char *cstar)
{
System::String^ cli = ::ToCliString(cstar);
if (cli == "abc")
{
MessageBox::Show("const char* -> CLI: OK");
}
const char* cstar2 …Run Code Online (Sandbox Code Playgroud) 我是一个正则表达式的新手,需要一个表达式:
匹配"an"和"AN"但不匹配"和"或"AND"并匹配此谓词中的"o"和"O"但不匹配"or"或"OR":
1和(2or3)AND(4OR5)的(6o7)AN(8O9)
基本上我无法弄清楚如何转换表达式:
var myRegEx = Regex("[0-9 ()]|AND|OR")
Run Code Online (Sandbox Code Playgroud)
进入一个"除了"之外,不区分大小写的表达.
无法使用正则表达式单词边界功能,因为谓词不需要包含空格.
(已经提供了两个答案后添加):我还需要知道匹配的索引,这就是为什么我假设我需要使用Regex.Match()方法.
谢谢!
这是我最终得到的:
private bool mValidateCharacters()
{
const string legalsPattern = @"[\d ()]|AND|OR";
const string splitPattern = "(" + legalsPattern + ")";
int position = 0;
string[] tokens = Regex.Split(txtTemplate.Text, splitPattern, RegexOptions.IgnoreCase);
// Array contains every legal operator/symbol found in the entry field
// and every substring preceeding, surrounded by, or following those operators/symbols
foreach (string token in tokens)
{
if (string.IsNullOrEmpty(token))
{
continue;
}
// Determine if the …Run Code Online (Sandbox Code Playgroud) 这是我的代码或WFP中的错误吗?使用VisualStudio 2013,以.NET 4.5.1为目标.
<Application x:Class="WPFDemo.MyApp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:WPFDemo.ViewModels">
<Application.Resources>
<vm:TestClass x:Key="tc" />
</Application.Resources>
</Application>
public partial class MyApp : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var mainWindow = new MainWindow();
mainWindow.Show();
}
}
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
object o = Application.Current.Resources["tc"];
}
}
Run Code Online (Sandbox Code Playgroud)
问题:o为空.
如果我添加至少一个任何类型的其他资源:
<Application.Resources>
<vm:TestClass x:Key="tc" />
<vm:TestClass2 x:Key="tc2" />
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
并且不会在任何地方进行任何其他更改,o作为TestClass的实例返回.
咦?
我正在学习WPF,MVVM Light和ViewModelLocator模式,并且遇到了我主窗口数据上下文的困难.
public class ViewModelLocator
{
public ViewModelLocator()
{
var mainModel = new MainModel();
Main = new MainViewModel(mainModel);
}
public MainViewModel Main { get; private set; }
public static ViewModelLocator Instance
{
get { return Application.Current.Resources["Locator"] as ViewModelLocator; }
}
}
Run Code Online (Sandbox Code Playgroud)
在我的app.xaml中:
<Application.Resources>
<viewModels:ViewModelLocator x:Key="Locator" />
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
当我在主窗口中设置数据上下文时使用:
DataContext="Binding Main, Source={StaticResource Locator}"
Run Code Online (Sandbox Code Playgroud)
它编译但是我绑定到xaml中其他地方的所有MainViewModel的属性都显示为红色,工具提示"无法解析符号".我以为我可以通过指定仅限设计者的数据上下文来解决这个问题:
<Window x:Class="WPFDemo.Windows.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:converters="clr-namespace:WPFDemo.Converters"
xmlns:local="clr-namespace:WPFDemo"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:models="clr-namespace:WPFDemo.Models"
xmlns:viewModels="clr-namespace:WPFDemo.ViewModels"
Title="MainWindow" Height="350" Width="525"
DataContext="Binding Main, Source={StaticResource Locator}"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance, Type=viewModels:MainViewModel,
IsDesignTimeCreatable=True}">
Run Code Online (Sandbox Code Playgroud)
但编译器不喜欢最后一行("字符','在这个位置意外",引用第一个逗号).注意我没有使用ExpressionBlend,但我认为我在课程中听说这行也将启用VisualStudio设计器:
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Run Code Online (Sandbox Code Playgroud)
如何在仍然允许Visual Studio在设计时识别绑定属性的同时使用ViewModelLocator?
c# ×6
.net ×2
properties ×2
wpf ×2
appdomain ×1
c ×1
c++ ×1
datacontext ×1
delegates ×1
dll ×1
lambda ×1
localization ×1
mvvm ×1
process ×1
python ×1
reflection ×1
regex ×1
windows ×1