小编Roh*_*ats的帖子

跨越不同的程序集版本传递类对象

场景如下 -

  • 我有一个组件说'MyAssembly'.接口说'IMyInterface'在此程序集中定义.
  • 在同一个程序集中,我有一个类(MyClass),其中的方法定义如下:

public void MyMethod(IMyInterface object){}

  • 现在,在我的项目中,我创建了一个具有相同名称和确切属性的接口,这些接口由"MyAssembly"中的"IMyInterface"接口公开.
  • 我有一个扩展此接口的类(我在我的项目中创建的那个),我希望将该类的对象作为参数传递给使用反射的不同程序集中的方法"MyMethod".

问题是 -

  • 当我尝试使用反射调用方法时,我得到了"无法将对象转换为类型IMyInterface"的异常.

代码 -

Assembly myAssembly = Assembly.LoadFrom("MyAssembly");
object classObject = myAssembly.CreateInstance("MyClass");
Type classType = myAssembly.GetType("MyClass");
MethodInfo myMethod = classType.GetMethod("MyMethod", BindingFlags.Instance);

// Creating an object of class in the latest assembly and need to pass this
// to method in assembly with different version.
ClassExtendingMyInterface obj= new ClassExtendingMyInterface ();

myMethod.Invoke(classObject, new object[] { obj});
Run Code Online (Sandbox Code Playgroud)

如果,我说得对,这是因为创建的对象是在不同的程序集中,并且该方法所期望的参数是它自己的程序集.

我想到的另一种方法是在类中创建我自己的动态方法,它将接受我的类的对象.

我试图通过Reflection.EmitRunSharp来google并动态创建自己的类.但是,我们只能在动态生成的程序集中使用它,但不能在现有程序集中创建动态方法或类.

我知道在运行时生成程序集并不是一个好方法.但我现在想不出任何事情.谢谢你的帮助.

c# reflection reflector assemblies reflection.emit

3
推荐指数
1
解决办法
2192
查看次数

用户代码未处理WCF未处理的FaultException

我正在尝试使用带有.net 4.5的VS 2012中的Telerik OpenAccess的WCF Plain服务.

我尝试了telerik开发人员手册并创建了服务和客户端.

在我放的IEntitiesModel服务接口中

[OperationContract]
[FaultContract(typeof(string))]
PersistentDto ReadPersistentDto(string dtoKey);
Run Code Online (Sandbox Code Playgroud)

在方法的EntitiesModel.SVC中,我首先使用了一个简单的构造:

public PersistentDto ReadPersistentDto(string dtoKey)
{
   throw new FaultException("test");
} 
Run Code Online (Sandbox Code Playgroud)

在Consumer的用户代码中,我为异常设置了一个catch.

现在的问题是:

每次调用服务时,Visual Studio都会在"throw new FaultException"中停止并显示错误消息,即用户代码未处理FaultException.如果我继续使用F5,则异常也会被消费者的用户代码捕获.

为什么它会在服务中停止?

根据我的理解,FaultException应该传递给服务的使用者.

我该怎么做才能正确抛出FaultException?

c# wcf telerik faultexception

3
推荐指数
1
解决办法
9787
查看次数

调用特定的打印机以打印C#WPF

我想调用特定的打印机在我的WPF应用程序中打印.我有三台打印机假设打印机1用于条形码打印打印机2用于厨房帐单打印打印机3用于宾客帐单打印打印机名称已保存在数据库中,而打印时我从DB获取打印机名称并希望从特定打印机打印,而不是默认打印机这是我的码

var v = new PrinterDAL().GetPrinterSettings();
try
{
   System.Threading.Thread thread = new System.Threading.Thread(new 
      System.Threading.ThreadStart(
            delegate()
            {   
                gridPrint.Dispatcher.Invoke(DispatcherPriority.Normal,
                    new Action(
                    delegate()
                    {
                            PrintDialog printDialog = new PrintDialog();
                            printDialog.PrintQueue = new PrintQueue(
                                new PrintServer(@"\\" + v.BarPrinter), "");
                            printDialog.PrintVisual(gridPrint, "");
                            this.Close();
                    }
                ));
            }
            ));
            thread.Start();
}
catch (Exception ex)
{
     Xceed.Wpf.Toolkit.MessageBox.Show(ex.Message, "", MessageBoxButton.OK, 
                                       MessageBoxImage.Error);
}
Run Code Online (Sandbox Code Playgroud)

我从这段代码中得到一个例外

"创建PrintServer对象时发生异常.Win32错误:打印机名称无效."

c# wpf

3
推荐指数
1
解决办法
4973
查看次数

WCF中的动态通道工厂

我在我的WCF服务中使用以下代码来调用另一个可能是也可能不是WCF服务的Web服务.

 ChannelFactory<IService1> myChannelFactory = new ChannelFactory<IService1>
                                                  (myBinding, myEndpoint);
Run Code Online (Sandbox Code Playgroud)

所以我希望在xml文件中有一些信息,我从中读取各种服务端点,并希望将绑定信息传递给通道工厂,并根据配置XML文件中的信息调用其他服务.

所以我想每次使用不同的服务合同信息动态生成通道工厂.

是否可以在渠道工厂中使用各种服务具有不同的接口?

换句话说,从上面的代码我有IService1但是当我从xml文件中读取另一个服务信息时,我想用另一个接口创建一个通道?

c# wcf channelfactory

3
推荐指数
1
解决办法
1982
查看次数

动态地使用不同的类

我有一节课:

 public class Test1
 {
     public void assignData(List<CustomClass> customData, string targetFieldName)
     {             
         for(int i=0; i<customData.Count; i++)
         {
             if(customData[i].targetFieldName)
             {
                 customData[i].targetFieldName = newValue;
             }   
         }
     }
 }

 List<customClass1> list1;
 List<customClass2> list2;
Run Code Online (Sandbox Code Playgroud)

customClass1和customClass2完全不同,但它们共享相同的字段'dateAdded'.我希望能够调用Test1.assignData(list1,"dateAdded")和Test1.assignData(list2,"dateAdded").并且list1和list2将更新.我怎样才能做到这一点?谢谢!

c# class list

3
推荐指数
1
解决办法
86
查看次数

什么版本的Visual Studio以Windows XP为目标?

我想使用WPF/C#.NET开发业务应用程序.该应用程序将具有许多现代gui小部件和功能,如可停靠视图和功能区.该应用程序还应在Windows XP以及Windows 7和8上运行.

我应该使用什么Visual Studio .NET版本?我听说在VS 2012/2013中你无法定位xp.是这样的吗?

c# windows wpf visual-studio

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

WPF 按钮 IsEnabled 检查多个绑定

我目前配置了以下数据绑定的 XAML 按钮的 IsEnabled 属性:

<Button Name="ThirdPartyPostoneButton" Content="Postpone"
        Click ="postponeThirdPartyUpdatesButton_Click" Margin="5,5,0,0"
        Height="25" IsEnabled="{Binding Item3.CanDefer}"/>
Run Code Online (Sandbox Code Playgroud)

我还需要添加一个检查IsEnabled="{Binding Item3.InstallSourceExists}"(换句话说,必须满足两个条件才能启用按钮)。我怎样才能做到这一点?

c# wpf

3
推荐指数
1
解决办法
2666
查看次数

绑定时 InitializeComponent 引发 StackOverflow 异常

试图理解数据绑定,这完全看起来像是一个菜鸟错误,但我不知道为什么会发生这种情况。

CS

namespace MuhProgram
{
    public partial class MainWindow : Window
    {
        public string foobar
        {
            get { return "loremipsum"; }
        }

        public MainWindow()
        {
            InitializeComponent();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

XAML:

<Window x:Class="MuhProgram.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MuhProgram"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
         <local:MainWindow x:Key="classMainWindow"/>
    </Window.Resources>

    <Grid>
        <Label Content="{Binding Source={StaticResource classMainWindow}, Path=foobar}"></Label>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

调试器指向带有 StackOverflowException 的方法InitializeComponent()调用MainWindow()

我也尝试在网格中设置DataContext属性"{StaticResource classMainWindow}",但效果是相同的。

c# data-binding wpf xaml

3
推荐指数
1
解决办法
2140
查看次数

异步任务挂起

我有以下代码:

public async Task SendPushNotificationAsync(string username, string message)
{
    var task = ApnsNotifications.Instance.Hub.SendAppleNativeNotificationAsync(alert, username);
    if (await Task.WhenAny(task, Task.Delay(500)) == task) {
       return true;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

我注意到它SendAppleNativeNotificationAsync是无限期挂起的(永远不会从包含方法返回),所以我试着告诉它在500ms后取消.但是仍然......对WhenAny现在的调用挂起并且我从未看到过return命中,导致消费者无限期地等待(这是调用此异步方法的同步方法,所以我调用.Wait()):

_commService.SendPushNotificationAsync(user.Username, notificationDto.PushContent).Wait(TimeSpan.FromSeconds(1));
Run Code Online (Sandbox Code Playgroud)

如何在一定时间后强制完成此操作,无论如何?

如果我只是"发射并忘记"而不是await执行任务会发生什么?

.net c# apple-push-notifications async-await azure-notificationhub

3
推荐指数
1
解决办法
787
查看次数

x上升到幂0不会评估为1

我创建了做单位转换类,具体字节来千字节,兆字节,千兆,等我有一个enumB通过PB,但由于某种原因1024^0不返回1,它不是正确地从字节转换为字节或字节为千字节,等等.

这是我的班级:

public static class UnitConversion
{
    /// <summary>
    /// 1024^n
    /// </summary>
    public enum ByteConversionConstant
    {
        B = 0,
        KB = 1,
        MB = 2,
        GB = 3,
        TB = 4,
        PB = 5
    }

    public static string GetValueFromBytes(long bytes,
                            ByteConversionConstant constant)
    {
        int n = (int)constant;
        long divisor = 1024^n;
        return (bytes / divisor).ToString() + 
               Enum.GetName(typeof(ByteConversionConstant), constant);
    }
}
Run Code Online (Sandbox Code Playgroud)

以下语句应返回与之完全相同的值fileInfo.Length,但由于1024^0未返回1,因此它显示的是千字节数.注意,我将GetValueFromBytes方法全部放在一行中,但是我把它分开来看看可能导致错误计算的原因. …

c# math unit-conversion

1
推荐指数
3
解决办法
655
查看次数

禁用复选框而不变灰

在wpf表单中,我有一个复选框,需要禁用而不会显示为灰色.

x.xaml

<CheckBox Content="check" Name="AP" DockPanel.Dock="Top"
          IsChecked="{Binding Path=check}"    
          Margin="0,0,6,8" />
Run Code Online (Sandbox Code Playgroud)

我试过哪个不行

wpf wpf-controls wpf-4.0

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

在迭代IEnumerable时从集合中删除项目

我有以下代码:

foreach (var bar in dataFromDataFeed.Where(bar => bar.Key < fromTicks
                                                  || bar.Key > toTicks))
{
   dataFromDataFeed.Remove(bar.Key);
}
Run Code Online (Sandbox Code Playgroud)

这是安全还是我需要将IEnumerable转换foreachDictionary<T,U>第一个?

谢谢.

c# iteration ienumerable list

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