Click here to Skip to main content
15,358,470 members
Home / Discussions / C#
   

C#

 
QuestionReading username Pin
Luis M. Rojas1-Dec-21 4:45
MemberLuis M. Rojas1-Dec-21 4:45 
AnswerRe: Reading username Pin
lmoelleb3-Dec-21 18:36
Memberlmoelleb3-Dec-21 18:36 
QuestionHow can I make the timer stay active after the program is closed? Pin
Duke Jason30-Nov-21 19:34
MemberDuke Jason30-Nov-21 19:34 
AnswerRe: How can I make the timer stay active after the program is closed? Pin
OriginalGriff30-Nov-21 20:09
mveOriginalGriff30-Nov-21 20:09 
AnswerRe: How can I make the timer stay active after the program is closed? Pin
Pete O'Hanlon30-Nov-21 21:24
mvaPete O'Hanlon30-Nov-21 21:24 
AnswerRe: How can I make the timer stay active after the program is closed? Pin
Dave Kreskowiak1-Dec-21 1:20
mveDave Kreskowiak1-Dec-21 1:20 
AnswerRe: How can I make the timer stay active after the program is closed? Pin
Gerry Schmitz1-Dec-21 18:09
mveGerry Schmitz1-Dec-21 18:09 
QuestionCode cast InvalidCastException when it "Shouldn't" Pin
Evilfish200029-Nov-21 19:28
MemberEvilfish200029-Nov-21 19:28 
I am working with EFT solutions in the LSRetail LSOne POS, and one of my customers has an issue that I cannot figure out why happens.

I have a class that has the following declaration:
C#
public class MyEFTService : EFTBase, IEFTService, IMyInterface
{
    //Code omitted
}
The IMyInterface interface is one I have added so I can execute some added functionality to the class from another location. I do that like this:
C#
public IPosTransaction RunTask()
{
    var service = Services.EFTService(entry);
    var dxcpcEft = (IMyInterface)service;

    //rest of code omitted
}
The Services.EFTService(entry) call returns the instance of MyEFTService created during startup, which I then cast to my IMyInterface and I can then do the calls I need to. This works on my machine, on my testers machine, and any co-workers machine. But it's here my customer has an issue. When we try to cast the instance to the IMyInterface interface on his machine we get an InvalidCastException. In debug, if I look at the service variable and browse through the object information, I can see variables that only my version of EFTBase has and I then know that it does have the IMyInterface as well.

We also tried to execute service is IMyInterface in the immediate debug window of visual studio and it returns true. So, as far as I can see, the service variable DOES have the IMyInterface and visual studios Immediate debug window confirms it. We also tried to run service.GetType(), and that too returns information related to MyEFTService class:
{Name = "EFTService" FullName = "LSOne.Services.EFT.EFTService"}
    Assembly: {LSOne.Services.EFT.MY_EFT, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}
    AssemblyQualifiedName: "LSOne.Services.EFT.MyEFTService, LSOne.Services.EFT.MY_EFT, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
    Attributes: Public | BeforeFieldInit
    BaseType: {Name = "EFTBase" FullName = "LSOne.Services.EFT.Common.EFTBase"}
    ContainsGenericParameters: false
    CustomAttributes: Count = 0
    DeclaredConstructors: {System.Reflection.ConstructorInfo[1]}
    DeclaredEvents: {System.Reflection.EventInfo[0]}
    DeclaredFields: {System.Reflection.FieldInfo[15]}
    DeclaredMembers: {System.Reflection.MemberInfo[75]}
    DeclaredMethods: {System.Reflection.MethodInfo[52]}
    DeclaredNestedTypes: {System.Reflection.TypeInfo.<get_DeclaredNestedTypes>d__23}
    DeclaredProperties: {System.Reflection.PropertyInfo[3]}
    DeclaringMethod: '((System.RuntimeType)service.GetType()).DeclaringMethod' threw an exception of type 'System.InvalidOperationException'
    DeclaringType: null
    FullName: "LSOne.Services.EFT.MyEFTService"
    GUID: {2804dbd4-c09a-3951-820b-ce4db1b72a55}
    GenericParameterAttributes: '((System.RuntimeType)service.GetType()).GenericParameterAttributes' threw an exception of type 'System.InvalidOperationException'
    GenericParameterPosition: '((System.RuntimeType)service.GetType()).GenericParameterPosition' threw an exception of type 'System.InvalidOperationException'
    GenericTypeArguments: {System.Type[0]}
    GenericTypeParameters: {System.Type[0]}
    HasElementType: false
    ImplementedInterfaces: {System.Type[3]}
    IsAbstract: false
    IsAnsiClass: true
    IsArray: false
    IsAutoClass: false
    IsAutoLayout: true
    IsByRef: false
    IsCOMObject: false
    IsClass: true
    IsConstructedGenericType: false
    IsContextful: false
    IsEnum: false
    IsExplicitLayout: false
    IsGenericParameter: false
    IsGenericType: false
    IsGenericTypeDefinition: false
    IsImport: false
    IsInterface: false
    IsLayoutSequential: false
    IsMarshalByRef: false
    IsNested: false
    IsNestedAssembly: false
    IsNestedFamANDAssem: false
    IsNestedFamORAssem: false
    IsNestedFamily: false
    IsNestedPrivate: false
    IsNestedPublic: false
    IsNotPublic: false
    IsPointer: false
    IsPrimitive: false
    IsPublic: true
    IsSealed: false
    IsSecurityCritical: true
    IsSecuritySafeCritical: false
    IsSecurityTransparent: false
    IsSerializable: false
    IsSpecialName: false
    IsUnicodeClass: false
    IsValueType: false
    IsVisible: true
    MemberType: TypeInfo
    MetadataToken: 33554438
    Module (System.Reflection.MemberInfo): {LSOne.Services.EFT.MY_EFT.dll}
    Module: {LSOne.Services.EFT.MY_EFT.dll}
    Name: "MyEFTService"
    Namespace: "LSOne.Services.EFT"
    ReflectedType: null
    StructLayoutAttribute: {System.Runtime.InteropServices.StructLayoutAttribute}
    TypeHandle: {System.RuntimeTypeHandle}
    TypeInitializer: null
    UnderlyingSystemType: {Name = "EFTService" FullName = "LSOne.Services.EFT.EFTService"}

But when we try to code line that casts to the interface, the code fails with an InvalidCastException. We briefly thought that the POS system might not have loaded MyEftService correctly, but we can do operations on it without any issue, so that is not the case.

How come that, on my customer's machine, it cannot cast the service variable to IMyInterface, when all investigations indicate that the service variable does contain an object that inherits IMyInterface, and it works on any other machine? Do you have any suggestions that we can try out?

I am aware that the code is not particularly "safe" in terms of casting and should probably be protected some more. That is in the works.
AnswerRe: Code cast InvalidCastException when it "Shouldn't" Pin
OriginalGriff29-Nov-21 21:24
mveOriginalGriff29-Nov-21 21:24 
GeneralRe: Code cast InvalidCastException when it "Shouldn't" Pin
Evilfish200030-Nov-21 2:24
MemberEvilfish200030-Nov-21 2:24 
GeneralRe: Code cast InvalidCastException when it "Shouldn't" Pin
OriginalGriff30-Nov-21 2:50
mveOriginalGriff30-Nov-21 2:50 
GeneralRe: Code cast InvalidCastException when it "Shouldn't" Pin
Evilfish200030-Nov-21 22:05
MemberEvilfish200030-Nov-21 22:05 
GeneralRe: Code cast InvalidCastException when it "Shouldn't" Pin
OriginalGriff30-Nov-21 22:19
mveOriginalGriff30-Nov-21 22:19 
GeneralRe: Code cast InvalidCastException when it "Shouldn't" Pin
Eddy Vluggen30-Nov-21 8:02
professionalEddy Vluggen30-Nov-21 8:02 
AnswerRe: Code cast InvalidCastException when it "Shouldn't" Pin
#realJSOP29-Nov-21 23:25
mva#realJSOP29-Nov-21 23:25 
Questionwhile run the code got no such element found exception seleniumc# Pin
Member 1407570327-Nov-21 20:01
MemberMember 1407570327-Nov-21 20:01 
AnswerRe: while run the code got no such element found exception seleniumc# Pin
OriginalGriff27-Nov-21 20:17
mveOriginalGriff27-Nov-21 20:17 
QuestionList-box item of PropertyGrid in C# could showing only once Pin
wuxianzhong26-Nov-21 2:40
Memberwuxianzhong26-Nov-21 2:40 
AnswerRe: List-box item of PropertyGrid in C# could showing only once Pin
OriginalGriff26-Nov-21 5:01
mveOriginalGriff26-Nov-21 5:01 
QuestionRe: List-box item of PropertyGrid in C# could showing only once Pin
Eddy Vluggen30-Nov-21 7:59
professionalEddy Vluggen30-Nov-21 7:59 
QuestionI want to make 2 Process Synchronization in C# with Mutex Pin
Bharat Bodage24-Nov-21 20:17
MemberBharat Bodage24-Nov-21 20:17 
AnswerRe: I want to make 2 Process Synchronization in C# with Mutex Pin
OriginalGriff24-Nov-21 21:04
mveOriginalGriff24-Nov-21 21:04 
AnswerRe: I want to make 2 Process Synchronization in C# with Mutex Pin
Richard Deeming24-Nov-21 21:31
mveRichard Deeming24-Nov-21 21:31 
QuestionSource of Integral Calculation Program Using Random Numbers Generation (Kaufman) Pin
Anvar Vaezi23-Nov-21 1:40
MemberAnvar Vaezi23-Nov-21 1:40 
AnswerRe: Source of Integral Calculation Program Using Random Numbers Generation (Kaufman) Pin
OriginalGriff23-Nov-21 3:21
mveOriginalGriff23-Nov-21 3:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.