天道酬勤,学无止境

.net

Why is implicit conversion allowed from superclass to subclass?

Can someone tell me why the line with "//Compiles" compiles, and why the line with "//Doesn't Compile" does not? I don't understand why A would be implicitly convertible to B, not the other way round. public class SomeClass { static public void Test() { AClass a = new AClass(); BClass b = new BClass(); a = b; // Compiles b = a; // Doesn't compile } } public class AClass { public void AMethod() { Console.WriteLine("AMethod"); } } public class BClass : AClass { public void BMethod() { Console.WriteLine("BMethod"); } } thanks!

2021-06-15 18:15:46    分类:问答    c#   .net   inheritance   type-conversion   implicit-conversion

Try-Catch and “Continue” - is this possible?

I have a section in my code where I am querying all SQL Server Databases on my network. I am first trying to use a SQL Login to access the SQL Server Instance but if that fails then I want to try connecting using my Windows Credentials. After that if I still can't connect then I want the code to fail and then notify the user. So I guess what I am asking is how can I loop back from inside of a Try-Catch block to the line just above the Try-Catch block: String conxString = @"Data Source=SQLInstance1;User ID=FOO;Password=BAR;"; bool secondTime = false; using (SqlConnection sqlConx = new

2021-06-15 18:12:05    分类:问答    c#   .net   try-catch

How to resolve a 'Bad packet length' error in SSH.NET?

I'm trying to upload files via SFTP, using SSH.NET. SFTPClient.Connect() throws an SshConnectionException with the message, "Bad packet length 1302334341." This is my code: static void Main() { try { SftpClient sftp = new SftpClient(server, 22, userID, password); sftp.Connect(); } catch(Exception e) { Console.WriteLine(e.Message); } } I saw in another discussion that this probably has to do with encryption. I'm using AES, and I have a host key. I don't understand how to enter the encryption, though. Based on that discussion, I was playing with this: ConnectionInfo connectionInfo = new

2021-06-15 18:10:14    分类:问答    c#   .net   ssh   sftp

Can I emulate MS Compute Cluster Server on my dev machine?

I have a project for a client that will consist of managing jobs on a MS Compute Cluster. I will be developing the application outside of their network, and would like a way to develop/debug my app without the need to be on their network. I am developing the app in C#, and all I have so far is the Microsoft Compute Cluster Pack SDK.

2021-06-15 18:07:37    分类:问答    c#   .net   cluster-computing   hpc

How do I sort a generic list based on a custom attribute?

I am working in c#.NEt 2.0. I have a class, let's say X with many properties. Each properties has a custom attribute, an interger, which I planned to use to specify its order int he final array. Using reflection I read through all of the properties and group the values and place them into a generic list of properties. This works and I can grab the values. But the plan was SORT the list, based on the custom attribute placed on each property, and finally read out the propery values, already ordered, into a string.

2021-06-15 18:07:28    分类:问答    c#   .net   generics   reflection   attributes

Does GetType() and typeof() constitute reflection?

In C# reflection invariably starts with: myInstance.GetType(); Or: typeof(MyType); To get the Type, then when one queries the info about the type e.g. getting properties, fields, attributes etc. they are certainly performing reflection. However are the above calls reflection themselves? I suppose in a academic sense the answer is yes - because you are reflecting on the type. So my second part to the question is: is it evaluated at run time and does it perform a heap allocation the first time? (I know subsequent calls to GetType() on the same type return the same Type instance so .NET must

2021-06-15 18:03:12    分类:问答    c#   .net

How to exclude specific type from json serialization

I am logging all requests to my WCF web services, including the arguments, to the database. This is the way I do it: create a class WcfMethodEntry which derives from PostSharp's aspect OnMethodBoundaryAspect, annotate all WCF methods with WcfMethodEntry attribute, in the WcfMethodEntry I serialize the method arguments to json with the JsonConvert.SerializeObject method and save it to the database. This works ok, but sometimes the arguments are quite large, for example a custom class with a couple of byte arrays with photo, fingerprint etc. I would like to exclude all those byte array data

2021-06-15 18:01:12    分类:问答    c#   .net   wcf   json.net   postsharp

How to detect when a hotkey (shortcut key) is pressed

How do I detect when a shortcut key such as Ctrl + O is pressed in a WPF (independently of any particular control)? I tried capturing KeyDown but the KeyEventArgs doesn't tell me whether or not Control or Alt is down.

2021-06-15 17:58:16    分类:问答    .net   wpf   keyboard-shortcuts   hotkeys

How to show a custom 404 page in ASP.NET without redirect?

When a request is 404 in ASP.NET on IIS 7 i want a custom error page to be displayed. The URL in the address bar should not change, so no redirect. How can i do this?

2021-06-15 17:53:12    分类:问答    c#   .net   asp.net   asp.net-mvc   vb.net

What is the difference between a COM string (BSTR) and a .NET string?

Is it just the way the bytes are combined to "encode" the data? I'm curious because I wonder how an RCW automatically takes a .NET string and transforms it into a COM BSTR. I'm guessing it just forms a valid COM BSTR transformed from the .NET string. Related: Could I construct my own valid BSTR using a byte type in .NET?

2021-06-15 17:52:13    分类:问答    .net   string   com   rcw