Permission class in vb.net - vb.net

creating an assembly named Assembly1. Assembly1 contains a public method.
The global cache contains a second assembly named Assembly2. You must ensure that
the public method is only called from Assembly2. Which permission class should you use?

It is impossible to say "method can be used only by Assembly2". But, if I understood you correctly and your goal is to prevent calls from any other assembly, except for Assembly1 or Assembly2, I have an answer.
In .NET you can do this using some trick. You can make your public method Friend (can be used only by Assembly1). And then use Friendly Assembly mechanism. After that you will have method that could be called only from Assembly1 or Assembly2.

Related

How to use Activator.CreateInstance to instantiate an external class?

I have an application where I want to instantiate a class that is completely outside the application, perhaps written at a later date by a third party. So the class cannot be known, but the interfaces in the class are known. So I want to use late binding.
In my code (VB.NET), I have the following:
Dim a As Object
a = Activator.CreateInstance("MyNameSpace.CustomClass", "")
MsgBox(a.Name)
I get an exception at the second line: Could not load file or assembly 'MyNameSpace.CustomClass' or one of its dependencies. The system cannot find the file specified. even though the assembly is in the same folder as the executable. I can't use Type.GetType() because the type is not known to the calling assembly.
You need the CreateInstanceFrom method.
var typeReference = Activator.CreateInstanceFrom(assemblyPath,
fullyQualifiedClassName);
But, for me, MEF would be a better solution as you can bind the Import/Export on the interface.

Use of VB.NET class in VB6 via interop requires class to have constructor?

When I try to create a VB.NET object via interop in VB6, I have noticed I get this error if my VB.NET class doesn't have a constructor:
Error 430 - Class doesn't support automation
All I have to do is put an empty constructor in the VB.NET class, eg:
Public Sub New()
End Sub
and the error is avoided. Is this the expected behaviour?
VB6 creates objects through COM, using the class factory for a COM coclass. The underlying method is IClassFactory::CreateInstance(). This method does not permit passing any arguments to the factory. It therefore follows that the [ComVisible] .NET class must have a constructor that doesn't take any arguments.
.NET already creates a default constructor for a class, unless you specify a constructor yourself that takes arguments. Which will never be used, you might as well remove it. Now you also don't need the empty default constructor anymore.

Where is VB.NET's MsgBox function defined?

This is really more of an academic question, but where is this function defined? Within .NET, I'm used to working in an object oriented manner. However, if I define a VB.NET class as follows:
Public Class foo
Public Sub showmessagebox()
Dim i As Integer
i = MsgBox("Message")
End Sub
End Class
Is MsgBox defined in a class? I am not required to reference a static class or inherit from another class. I'm not even required to import a namespace. I did find this link from msdn. But my question remains, where is this defined and how does the CLR just load up a function?
If you enter MsgBox into Visual Studio and hit F12, you will see that it is in the Microsoft.VisualBasic namespace, in the Interaction module.
In fact, this information is also available (although a bit hidden) at the bottom of the MSDN page you referenced:
Namespace: Microsoft.VisualBasic
Module: Interaction
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)
If you look into the References section of your project properties, you'll see that the Microsoft.VisualBasic namespace is automatically imported. Since Interaction is a module, you can use its methods without having to qualify the module name (as opposed to static/Shared methods of a class).
As a side note: If you add a reference to Microsoft.VisualBasic.dll to a C# project, you can use Microsoft.VisualBasic.Interaction.MsgBox("Hello World"); there as well (although most C# users will prefer using the MessageBox class).

C++ virtual (sealed) function

I am using classes from a dll in my C++ project. All is working fine, until...
When trying to call a certain method (listed in the object browser), I am getting an error that this method is not a member of the namespace.
Upon investigation, I noticed that this method is listed as "virtual void x() sealed".
Is there a way to make a call to such a function?
For future reference, I just received a response from the enterprise library support team. They posted a link to the following:
Managed C++ and IDisposable
I'm writing some code using the new Managed C++/CLI syntax and I ran into this error:
error C2039: 'Dispose' : is not a member of 'System::IDisposable'
the code I started with was this:
image->Dispose(); // image implements IDisposable
which gave me the same compiler error, so I wanted to eliminate a class/namespace error so I rewrote it as this:
((IDisposable ^)image)->Dispose();
Which gave the above error. Yikes!
Here's the fix:
use delete. Managed C++ now hides Dispose() inside the finalizer. Just delete the object, it handles the rest. Freaky.
This really works!!!!
Sealed in a C++ CLI keyword (managed C++) specific to .NET and not C++ in general.
sealed on a function means that you can't override that method in a derived type.
sealed does not mean that you can't call the function, I'm guessing your function is private.
I don't see why it being virtual and sealed should in itself prevent you from calling the function. According to MSDN, the sealed keyword is specifically meant for virtual methods anyway.
Is there any more information you can give about the function in question and how you are trying to use it?

Problem overriding methods in VB.NET (Error BC30284)

I have an overridable sub in my base class
Project1:
Public Class BaseClass
Protected Overridable Sub MySub(ByVal Parameter as MyType)
End Class
Project2:
Public Class DerivedClass
Inherits BaseClass
Protected Overrides Sub MySub(ByVal Parameter as MyType)
End Class
MyType is a type that comes from external COM library. When I'm trying to override it in a derived class, I'm getting
error BC30284: sub 'MySub' cannot be declared 'Overrides' because it does not override a function in a base class
I've added the required COM reference to both projects containing base and derived classes.
Any idea how to get rid of this error? I'm using VS2005 and .NET 2.0
Edit: Every other override is working fine, I'm only getting error if I'm using referenced COM types as parameters. If I change Parameter to Object, overriding works fine.
Have you considered or tried using TlbImp.exe to generate a static DLL from the COM type library, and reference that from both projects (instead of using a COM reference) to make sure they are referring to exactly the same thing? TlbImp is included with Visual Studio, but I can't find it on my system with only Visual Studio Express installed, so if you're using express, you might have to go hunting for it (the linked page may or may not have the version you want). I suspect that if each project has their own COM reference, Visual Studio may be creating a separate COM wrapper for each project and the generated COM wrappers may not entirely agree with each other when it comes to generated GUIDs and whatnot. So by creating a and forcing the use of a single wrapper you may be able to eliminate that as a possible problem.
Rather than using TlbImp, another option is to have a separate project where you encapsulate the MyType in a .NET class and include that project in both your samples.
So you would end up with an intermediate MyDotNetType which would take as a constructor argument Mytype (the COM object) and expose it out as a read-only property.
Then the MySub call, would take the MyDotNetType as an argument.
Kind Regards
Noel
Please check the signature of the Function in both base class and derived class, if you have different agruments or data type o any arguments is not matched. Then you'll get this type of error. Simple please check the function name, argument name and data type. It worked me. I hope this answer will be helpful.
Thanks,
Ramu V