|
Try to create both Contructors to each Form Class, maybe it helps!
|
|
|
|
|
Dear All,
I have created billing system for Motorcycle service centers using c# .
I wan't to add a feature, when the user print the final invoice, customer will receive a sms from the name of the service dealer which including thanks for the coming to the service.
and also after three months of the time that he last came to the service, system has to send a message to the customer that saying please come for the next service bla bla etc.
Can any one guide me how to do this? for this feature I planned to install a 4G Dongle and sim card from local cellular service to the service dealer's pc.
|
|
|
|
|
What have you tried?
Where are you stuck?
What help do you need?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Using a dongle is probably over-complicating things and certainly impacts scalability (if you need a second PC you need another dongle + SIM + contract etc).
There are plenty of webservices out there that will send an SMS for you, and give you additional management info / usage data etc.
Depending on volumes you might even find a free provider, though often the free services (understandably) inject adverts into your text. Not knowing which country you're in I can't recommend a specific service...
Once you have a webservice interface, it's very simple to integrate into your C# project and no worries about hardware etc. Some of these services even allow you to schedule texts for future dates - don't know if up to 3 months in advance, though!
Google "send sms from webservice"
Cheers,
Derek
|
|
|
|
|
I'm facing a new situation involving a program, written in .NET Framework 4.0/C#, which has to encode/decode and sign messages to be sent/received in a particular kind of WAN. This is the current scenario, which is working fine. We have a .p12 file (which contains the sender certificate) and a .cer file (which contains the receiver certificate), which are both installed in the pc. The scenario is working using SHA1 and PKCS#7 at 1024 bit, so the following piece of code works:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography;
using System.Windows;
using System.Windows.Controls;
using System.Collections;
private static string senderCert = "sendername";
private static string receiverCert = "receivername";
public static byte[] Verify(this byte[] signedData)
{
X509Certificate2 certPub = GetReceiverCert();
if (certPub == null) return null;
ContentInfo decodeContentInfo = new ContentInfo(signedData);
SignedCms decodeCMS = new SignedCms(decodeContentInfo, false);
try
{
decodeCMS.Decode(signedData);
SignerInfo signerInfo = decodeCMS.SignerInfos[0];
X509Certificate2Collection certCollection = new
X509Certificate2Collection(certPub);
return decodeCMS.ContentInfo.Content;
}
catch (CryptographicException err)
{
Logger.Log(err);
return null;
}
}
private static X509Certificate2 GetSenderCert()
{
X509Store storeMy = new X509Store(StoreName.My, StoreLocation.CurrentUser);
storeMy.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certColl = storeMy.Certificates.Find
(X509FindType.FindBySubjectName, senderCert, false);
if (certColl.Count == 0)
return null;
storeMy.Close();
return certColl[0];
}
private static X509Certificate2 GetReceiverCert()
{
X509Store storeMy = new X509Store(StoreName.My,
StoreLocation.CurrentUser);
storeMy.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certColl = storeMy.Certificates.Find
(X509FindType.FindBySubjectName, receiverCert, false);
if (certColl.Count == 0)
return null;
storeMy.Close();
return certColl[0];
}
public static byte[] Sign(this byte[] data)
{
X509Certificate2 certificate = GetSenderCert();
if (certificate == null) return null;
if (data == null)
throw new ArgumentNullException("data");
if (certificate == null)
throw new ArgumentNullException("certificate");
ContentInfo content = new ContentInfo(data);
signedCms = new SignedCms(content, false);
CmsSigner signer = new CmsSigner(certificate);
signer.IncludeOption = X509IncludeOption.EndCertOnly;
signedCms.ComputeSignature(signer);
return signedCms.Encode();
}
As I said before, this code works fine under the given scenario (SHA1+PKCS#7 at 1024 bit). Now the client asks an update of the crypthography, in particular using SHA256 instead of SHA1 and PKCS#10 instead of PKCS#7 with key length 2048 bit instead of 1024.
According to your experience, what should I do to get the new requirements?
The only thing I'm sure is that I cannot use a NET Framework greater than this one because the devices are old XPs and they cannot be changed for company issues. So I cannot use new libraries released for .NET 4.7.1 to solve my problem.
I suppose that the new certificates will be released as the previous method, a file for sender, another one for receiver, to be installed in the machine.
Honestly I haven't a lot of experience with crypto and I would like to find an easy path which can follow the logic of the current methods, since the fact their scope wouldn't be changed, but just the signing approach.
I have no other limitations in using other libraries, pinvoke and other stuff, if it can really help.
Thanks to all of you for your support.
Any suggestion is appreciated. Regards
|
|
|
|
|
Im trying to publish a message that should communicate between classes:
public sealed class EventMessager : IEventMessager
{
private readonly Dictionary<Type, object> subscriberLookup = new Dictionary<Type, object>();
public IObservable<T> Observe<T>()
{
if (!subscriberLookup.TryGetValue(typeof(T), out object subject))
{
subject = new Subject<T>();
subscriberLookup.Add(typeof(T), subject);
}
return ((ISubject<T>)subject).AsObservable();
}
public void Publish<T>(T @event)
{
if (subscriberLookup.TryGetValue(@event.GetType(), out object subject))
{
((Subject<T>)subject).OnNext(@event);
}
}
}
This works well, except for the time I wanted to use Reflection in order to initate a new Message class like so:
eventMessager.Publish(Activator.CreateInstance(w[0]));
It complains that it cannot cast the subject of class type that w is as an Subject Object.
It will work as expected if I just say new MyClass instead of the activator.
I really cant find any solutions online either, so does anyone know what is going on here?
modified 6-Sep-18 2:05am.
|
|
|
|
|
I'm a bit tired here so I'm having a problem groking what you want to achieve here but one thing that stands out is that you are calling a generic Publish method with a reflected object and expecting Subject<t> to know what type the subject is.
This space for rent
|
|
|
|
|
Activator.CreateInstance(Type) returns an Object .
Activator.CreateInstance Method (System) | Microsoft Docs[^]
As a result, you are calling eventMessager.Publish<object>(instanceOfYourType) .
That uses the run-time type of the parameter to find the subscriber. This will be a Subject<YourClass> instance.
You then try to cast that instance to Subject<object> , which fails because the Subject<T> class is not covariant on its type parameter.
Covariance and Contravariance in Generics | Microsoft Docs[^]
You can't even get away with casting the subscriber to the IObserver<T> interface, since that interface is contravariant.
You're going to need to use reflection to invoke the method. I'd suggest using a separate method to avoid making the regular method slower:
public void PublishNew(Type eventType)
{
if (subscriberLookup.TryGetValue(eventType, out object subject))
{
object @event = Activator.CreateInstance(eventType);
Type t = typeof(IObserver<>).MakeGenericType(eventType);
MethodInfo onNext = t.GetMethod("OnNext");
onNext.Invoke(subject, new[] { @event });
}
}
...
eventMessager.PublishNew(w[0]);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
modified 5-Sep-18 13:17pm.
|
|
|
|
|
Ah, that works perfectly, thank you.
|
|
|
|
|
Richard Deeming wrote: I'd suggest using a separate method to avoid making the regular method slower:
I found some other lower level code that the author claimed was 10 times faster than Activator.CreateInastance:
public class ObjectCreateMethod
{
delegate object MethodInvoker();
MethodInvoker methodHandler = null;
public ObjectCreateMethod(Type type)
{
CreateMethod(type.GetConstructor(Type.EmptyTypes));
}
public ObjectCreateMethod(ConstructorInfo target)
{
CreateMethod(target);
}
void CreateMethod(ConstructorInfo target)
{
DynamicMethod dynamic = new DynamicMethod(string.Empty,
typeof(object),
new Type[0],
target.DeclaringType);
ILGenerator il = dynamic.GetILGenerator();
il.DeclareLocal(target.DeclaringType);
il.Emit(OpCodes.Newobj, target);
il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ldloc_0);
il.Emit(OpCodes.Ret);
methodHandler = (MethodInvoker)dynamic.CreateDelegate(typeof(MethodInvoker));
}
public object CreateInstance()
{
return methodHandler();
}
}
But you have solved my problem, so this does not bother me as much.
|
|
|
|
|
Hi Folks,
I have a referenced .dll in one of the projects in my solution, and from what I can tell during debugging most of it appears to be used and accessed without issue.
However, when calling a 'getModelDataContext' function from the model.designer.cs file that should be within the .dll, nothing seems to happen. It appears to not be reachable ('under the caret') when I try to jump to its definition. It is not breaking though when it hits this function, but it is not returning a valid data connection, and then a subsequent function call is using an empty data set and failing at that point.
Does anyone have any ideas how I can approach this please?
Many thanks,
AJ
|
|
|
|
|
Look at the source code (if you have it), or the documentation provided by whoever created the DLL. You should be able to collect more information from the debugger by single-stepping through the code.
|
|
|
|
|
Thank you, that's a good idea. I'll see if I can get into the source code for some further investigation.
All the best,
AJ
|
|
|
|
|
This was based on silly oversight on my part. Although I was unable to access the functions and files through the .dll itself. After looking through the source code I found that it was in fact accessing everything and the result it was producing was in fact perfectly fine as per the underlying code.
The issue truned out to be with the data I had been sent to test the API with as it did not match a suitable record in another system used to retrieve additional record data.
I'm still not sure why I was unable to navigate to the file in question through the .dll but thankfully, as I was able to get into the original sorce code, I managed to work aroung this.
Thank you Richard for your input!
AJ
|
|
|
|
|
Hello,
I am trying to get a leg up at my job, so this is not a homework assignment.
I need help to begin to understand Constructor Injection.
there are five types; Default, Parameterized, Copy, Static and Private. The constructor creates an instance for the class, and initializes Private fields.
Where would Injection be used?
What are the risks of modifications to the constructor, does it break the "target" code?
How do constructors allow for unit testing?
What is unit testing?
What book can you recommend to learn C#? I have some experience in C, Visual Studio, and way back Assembly.
Thanks in advance, I am sure I will have other questions also.
Tom.
|
|
|
|
|
You have many questions in one message.
I have created a "Zelda" example with lot of constructors. I hope you find it useful.
Hint: Start a new console application in C#, called ConsoleApplication1. Paste the code, set break points and debug.
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var maxArrows = Link.ArrowMaximum;
var link = new Link();
IWeapon weapon = new KokiriSword();
link = new Link(10);
link = new Link(weapon);
link = new Link("Go to Lost woods");
var fierceDeityLink = new FierceDeityLink();
}
}
public interface IWeapon
{
int Damage { get; }
}
public class NoWeapon : IWeapon
{
public int Damage { get; private set; }
public NoWeapon()
{
Damage = 0;
}
}
public class KokiriSword : IWeapon
{
public int Damage { get; private set; }
public KokiriSword()
{
Damage = 1;
}
}
public class BigGoronSword : IWeapon
{
public int Damage { get; private set; }
public BigGoronSword()
{
Damage = 100;
}
}
public class Link
{
public static int ArrowMaximum { get; private set; }
public int Rupies { get; private set; }
public bool RecuredZelda { get; private set; }
public string HintFromNavi { get; private set; }
public IWeapon Weapon { get; private set; }
static Link()
{
ArrowMaximum = 100;
}
public Link()
{
Rupies = 5;
Weapon = new NoWeapon();
}
private Link(bool recusedZelda)
{
RecuredZelda = recusedZelda;
}
public Link(int rupies)
{
Rupies = rupies;
}
public Link(IWeapon weapon)
{
Weapon = weapon;
}
protected Link(int rupies, bool recuedZelda, IWeapon weapon) : this(rupies)
{
RecuredZelda = recuedZelda;
Weapon = weapon;
}
internal Link(string hintFromNavi)
{
HintFromNavi = hintFromNavi;
}
}
public class FierceDeityLink : Link
{
public FierceDeityLink() : base (int.MaxValue, true, new BigGoronSword())
{
}
}
}
Answers for your other questions:
Where would Injection be used?
Lots of places. Depends on the logic of the code. Read here.
What are the risks of modifications to the constructor, does it break the "target" code?
Impossible to say without reviewing the code.
Try... and learn from mistakes.
How do constructors allow for unit testing?
Constructors are used to create objects from classes. That has nothing to do with unit testing.
What is unit testing?
Something which is very useful and you should adapt immediately. Read here.
|
|
|
|
|
Hello,
Even though webcam quality is 5mp , webcam seems and takes low quality of picture via below codes.
Is there any way to fix this and raise quality to maximum possible?
namespace DXApplication2
{
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
public Form1()
{
InitializeComponent();
}
private FilterInfoCollection webcam;
private VideoCaptureDevice cam;
private void Form1_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
webcam = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo VideoCaptureDevice in webcam)
{
comboBox2.Items.Add(VideoCaptureDevice.Name);
}
comboBox2.SelectedIndex = 0;
}
if (pictureBox3.Image != null)
{
iTextSharp.text.Image png2 = iTextSharp.text.Image.GetInstance(pictureBox3.Image, System.Drawing.Imaging.ImageFormat.Jpeg);
png2.Alignment = Element.ALIGN_CENTER;
if (png2.Height > png2.Width)
{
float percentage = 0.0f;
percentage = 400 / png2.Height;
png2.ScalePercent(percentage * 100);
}
else
{
float percentage = 0.0f;
percentage = 320 / png2.Width;
png2.ScalePercent(percentage * 100);
}
pdf1.Add(png2);
}
private void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap bit = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = bit;
}
private void button6_Click(object sender, EventArgs e)
{
pictureBox2.Image = pictureBox1.Image;
}
modified 13-Feb-19 21:02pm.
|
|
|
|
|
i need to get a picture (.BMP or image) convert to .dpx format. The DPX is Digital Picture Exchange , so how can i convert image to DPX format in C# ?
|
|
|
|
|
.NET (and thus C#) does not natively support DPX files, so to write them you will need to either write the code to convert and save them yourself, or use a prewritten library for that. This one seems well appreciated: GraphicsMagic @ CodePlex Archive[^] - I've not used it myself though.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
When some exception occurs in a class of the .Net framework, we see the public function of a public class called by our code only, the further details (private functions, or even non-public classes) used by the public function are hidden in the StackTrace of the exception object, when the exception occurred somewhere down there.
Now I have some functionality where I'd like to have such a feature, too:
For testing, we use MS Test. I added some extensions, e.g. "ThrowsAssert " (based on Asserting Exceptions in MSTest with Assert.Throws()[^]). When such an Assert fails, I get a long StackTrace like:
bei TestUtilities.ExceptionAssert.OnNoExceptionThrown[T](String _customMessage) in C:\Users\bernhard.hiller\SVN\Product-SW\trunk\C_TestUtilities\ExceptionAssert.cs:Zeile 29.
bei TestUtilities.ThrowsAssert.Throws[T](Action _task, String _expectedMessage, ExceptionMessageCompareOptions _messageOptions, ExceptionInheritanceOptions _inheritOptions, String _customMessage) in C:\Users\bernhard.hiller\SVN\Product-SW\trunk\C_TestUtilities\ThrowsAssert.cs:Zeile 42.
bei MyAssembly.Tests.MyClassTests.MyFunctionTest() in C:\Users\bernhard.hiller\SVN\Product-SW\trunk\MyAssemblyTests\MyClassTests.cs:Zeile 36.
or even longer...
Is there some way to "hide" all those functions of class ThrowsAssert and ExceptionAssert in the StackTrace?
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
As far as I'm aware, the only way to hide methods from the stack trace is to get the compiler to inline them - since they are no longer called, they are no longer available to stack tracing. Unfortunately for you, there is no way that I know of to force the compiler to inline a method - you can force it to not inline, but the reverse does not apply. I understand that you can (in v4.5 and later) hint that you might like it inlined: MethodImplOptions Enum (System.Runtime.CompilerServices) | Microsoft Docs[^] using the AgressiveInlining field, but that is just a suggestion, not an order!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Take the "stack trace", run it through a "string reader", and eliminate the entries you don't want (via a class or method "name" related to the exception).
I do all my "trace filtering" in the
Application_DispatcherUnhandledException
event handler.
At that point, I can even "continue execution" via "e.handled" (if the situation calls for it).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I got a class and I have created an array in the class
<pre>class myclass
{
string array = new string[5];
for (int i = 0; i< array.Length; i++)
{
array[i] = "whatever"
}
}
now I want to transfer this array into my main, maybe create new array at main and just transfer the info. how do i create and
string GetArray() in class to return an array, and how do I get the info into main. thanks
|
|
|
|
|
|