|
You're welcome. I'm glad I could help.
|
|
|
|
|
Out of interest, do you know what would happen in that example if the add method took a string rather than object? I don't although I'm pretty sure I've come up against this before.
Regards,
Rob Philpott.
|
|
|
|
|
Base on Pete O'Hanlon answer, I am pretty sure that it will call the first method.
Gonna try it.
|
|
|
|
|
Could be, but its veering towards the 'undefined' where what it does now may change with different versions of compiler/framework etc.
Regards,
Rob Philpott.
|
|
|
|
|
Looking at the C# specification, section 7.5.3.2:
7.5.3.2 Better function member[ ^]
... the following tie-breaking rules are applied, in order, to determine the better function member:
- If MP is a non-generic method and MQ is a generic method, then MP is better than MQ.
- ...
In other words, if two candidate methods are found, and one is generic but the other isn't, then the non-generic method is always chosen.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Outstanding.
Regards,
Rob Philpott.
|
|
|
|
|
It would call the string based method because that was the most specialised version it could call.
|
|
|
|
|
It's a really bad idea to do this for exactly the reason you describe here: it's totally unobvious which of those two methods will be called in what context. (I think Pete is right, I think the first will be called if and only if the parameter has a compile time static type of object and not a subclass, but that's confusing and unclear.)
|
|
|
|
|
I have more than 4 year experience so can i move to consultant profile, if yes please let me know , How can i do this? Give me some some ideas.
Thanks in Advance.
|
|
|
|
|
Pramod Kumar Singh 2010 wrote: Give me some some ideas
ahm, wrong forum?
Don't mind those people who say you're not HOT. At least you know you're COOL.
I'm not afraid of falling, I'm afraid of the sudden stop at the end of the fall! - Richard Andrew x64
|
|
|
|
|
Looking at your messages in CP, ... well, sorry, ... I think you mix up "time" and "experience".
|
|
|
|
|
Apply for some positions and go from there.
|
|
|
|
|
Moving into more design-oriented roles where you are in charge of higher level abstraction rather than being the code monkey with the typewriter is about knowledge and skill, not time spent bashing keys. So you should develop skills, pick up knowledge and demonstrate within your project teams that you are capable, so you have some design and architecture experience to stick on your CV when you start hawking yourself as a consultant.
|
|
|
|
|
Considering you just asked what a "extension method" was, you do NOT have 4 years experience in C#.
|
|
|
|
|
To be fair, he doesn't actually state he has 4 years of experience in .NET. It could be in Origami.
|
|
|
|
|
|
I have some CSV files that i import and place in Datatables, from there i need to make a comparison of the rows, case is that some of them are really big files so i have to make a lot of iterations which results in a really really bad performance, does anyone have an idea of what can i do to improve the performance? i did some filters but still same result. I´m kind a new in this please help me.
|
|
|
|
|
Hi,
Can you please give a snippet of your code? Maybe, the import and the "comparison of rows".
Thanks.
Don't mind those people who say you're not HOT. At least you know you're COOL.
I'm not afraid of falling, I'm afraid of the sudden stop at the end of the fall! - Richard Andrew x64
|
|
|
|
|
Hi, this is the function where i make some of the comparison, in this case i need to find if exist a difference between 2 fields of the tables.
DataView dv = new DataView(dt1);
dv.RowFilter = "[Design Data] NOT LIKE ''";
filesSTdt = dv.ToTable();
DataView dv2 = new DataView(filesSTdt);
int contRow = 0;
bool foundflag;
string designDataL, MpStatusF, MpStatusL, MpF, MpL;
string[] designDataF;
for (int k = 0; k < dt1.Columns.Count; k++)
{
dt3.Columns.Add(dt1.Columns[k].ToString(), Type.GetType("System.String"));
}
for (int i = 0; i < dt2.Rows.Count; i++)
{
designDataL = dt2.Rows[i]["DS-PZ"].ToString();
MpL = dt2.Rows[i]["MP"].ToString();
MpStatusL = dt2.Rows[i]["Status"].ToString();
dv2.RowFilter = "[Design Data] LIKE '" + designDataL + " A' AND [Change] = '" + MpL + "'";
filesSTdt = dv2.ToTable();
if (filesSTdt.Rows.Count > 0)
{
for (int j = 0; j < filesSTdt.Rows.Count; j++)
{
designDataF = filesSTdt.Rows[j]["Design Data"].ToString().Split(' ');
MpStatusF = filesSTdt.Rows[j]["Implementation Status"].ToString();
MpF = filesSTdt.Rows[j]["Change"].ToString();
if (designDataL == designDataF[0])
{
if (MpStatusL != MpStatusF)
{
dt3.ImportRow(filesSTdt.Rows[j]);
dt3.Rows[contRow]["Implementation Status"] = MpStatusL;
contRow = contRow + 1;
}
}
}
}
}
|
|
|
|
|
I sounds like you are using the datatable in c#, a very expensive construct, either change it to a List<> or even better move the data to a database and do the heavy lifting in there using stored procedures.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
How better could be the performance using lists vs using datatables?
|
|
|
|
|
Not a question that can be answered by anyone but you.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I have been looking for the anwer to this question for days now. I found that their is very little on smart card implementaion for C# but from alot of research I and alot of playing around I have finialy solved the question. Below is a working example of my smart card class
SmartCard.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Security.Permissions;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Security;
using System.ComponentModel;
namespace SmartCardApp
{
public class SmartCard
{
internal static int CertCredential = 1;
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool CredMarshalCredential(
int credType,
IntPtr credential,
out IntPtr marshaledCredential
);
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hWnd);
[StructLayout(LayoutKind.Sequential)]
internal struct CERT_CREDENTIAL_INFO
{
public uint cbSize;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public byte[] rgbHashOfCert;
}
public static string pin = null;
public static X509Certificate2 cert = null;
public struct UserLoginInfo
{
public string domain;
public string username;
public SecureString password;
}
public void SmartCardLogon()
{
cert = GetClientCertificate();
Form login = new Form();
login.Height = 100;
login.Width = 100;
login.MaximizeBox = false;
login.MinimizeBox = false;
login.ControlBox = false;
login.Name = "frmCaCLogin";
login.Text = "Enter Pin";
TextBox TextBox1 = new TextBox();
TextBox1.Name = "txtCaCLogin";
TextBox1.PasswordChar = '*';
login.Controls.Add(TextBox1);
Button b = new Button();
b.FlatStyle = FlatStyle.Flat;
b.Text = "Login";
b.Name = "butCacLogin";
b.Click += new EventHandler(b_Click);
login.Controls.Add(b);
login.Controls["butCacLogin"].Top += 20;
login.TopMost = true;
login.ShowDialog();
}
void b_Click(object sender, EventArgs e)
{
pin = Application.OpenForms["frmCaCLogin"].Controls["txtCaCLogin"].Text;
Application.OpenForms["frmCaCLogin"].Hide();
if (cert != null)
{
UserLoginInfo user = Login(cert);
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = Application.ExecutablePath;
psi.UserName = user.username;
psi.Password = user.password;
psi.UseShellExecute = false;
Process.Start(psi);
Application.Exit();
}
}
public UserLoginInfo Login(X509Certificate2 cert)
{
UserLoginInfo uli = new UserLoginInfo();
CERT_CREDENTIAL_INFO certInfo =
new CERT_CREDENTIAL_INFO();
certInfo.cbSize = (uint)Marshal.SizeOf(typeof(CERT_CREDENTIAL_INFO));
certInfo.rgbHashOfCert = cert.GetCertHash();
int size = Marshal.SizeOf(certInfo);
IntPtr pCertInfo = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(certInfo, pCertInfo, false);
IntPtr marshaledCredential = IntPtr.Zero;
bool result =
CredMarshalCredential(CertCredential,
pCertInfo,
out marshaledCredential);
string domainName = string.Empty;
string userName = string.Empty;
string password = string.Empty;
if (result)
{
domainName = String.Empty;
userName = Marshal.PtrToStringUni(marshaledCredential);
password = pin;
}
SecureString sc = new SecureString();
foreach (char c in pin)
{
sc.AppendChar(c);
}
uli.domain = Environment.UserDomainName;
uli.username = userName;
uli.password = sc;
return uli;
}
public static X509Certificate2 GetClientCertificate()
{
WindowsImpersonationContext impersonationContext = null;
IntPtr ptr = IntPtr.Zero;
X509Certificate2 certificate = null;
X509Certificate t = null;
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
try
{
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
if (store.Certificates != null && store.Certificates.Count > 0)
{
if (store.Certificates.Count == 1)
{
certificate = store.Certificates[0];
}
else
{
var certificates = X509Certificate2UI.SelectFromCollection(store.Certificates, "Digital Certificates", "Select a certificate from the following list:", X509SelectionFlag.SingleSelection, ptr);
if (certificates != null && certificates.Count > 0)
certificate = certificates[0];
}
}
}
finally
{
store.Close();
}
return certificate;
}
}
this is the code i use for the smartcard login button
form1.cs file
just ad a button to a form call it butCacLogin put all the code from the class above into a new class called SmartCard.cs then just run the code.
private void butCacLogin_Click(object sender, EventArgs e)
{
this.Hide();
SmartCard sc = new SmartCard();
sc.SmartCardLogon();
this.Close();
}
The form for the pin could use some decorating but this works like a charm. At first I tried to use wiondws LogonUser method and using impersantation however when trying to start the process back up with the new credentials it was getting either directory not found or file not found. I then decided to just pass the credentials I recived back from the cert directly to the process and that worked perfectly. I looked for days never found full code for this only bits and pieaces of code and never a good working example.
The reason I started this up is because you can't do a runas on a one click application by right clicking it. Then I figured out how to pass the username and password to the process but our network is cac enforced. Then I decided to find a way to enable the process to start with cac credentials. I hope this helps somone out because this is probally the only place you can find the full working code for C# every other place i looked was just pieaces of code.
modified 21-May-14 17:58pm.
|
|
|
|
|
This is the start of an interesting article (I have seen less work go into some). Expand on the reasons for your decisions, discuss the requirements, add a few screen shots and you will do the community a service by publishing this as an article.
The effort of publishing is rewarded by you getting a better understanding as you try and explain it to others. Oh and Author points of course.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Very interesting but this forum is really not the place; you should write an article about it and publish it that way.
|
|
|
|