C# 如何实现一个抽象类的接口,这个抽象类是约束的?
几天前,我尝试使用(引用)ms access vba中没有接口的QRCode库.net,但没有成功。所以我做了一些研究,这里的人指导我。 所以我决定按照以下步骤制作界面 我已经能够看到intellisense并创建其他类的一些对象和变量,但是我在一个类中遇到了麻烦,在这个类中我看到了Dispose(),但我不能在ms access中使用新的:C# 如何实现一个抽象类的接口,这个抽象类是约束的?,c#,vba,com,C#,Vba,Com,几天前,我尝试使用(引用)ms access vba中没有接口的QRCode库.net,但没有成功。所以我做了一些研究,这里的人指导我。 所以我决定按照以下步骤制作界面 我已经能够看到intellisense并创建其他类的一些对象和变量,但是我在一个类中遇到了麻烦,在这个类中我看到了Dispose(),但我不能在ms access中使用新的: [Dim QRCD as New QRCode] 我收到一个错误,使用New无效,但我可以用以下行声明: Dim QRCD as QRCode [我
[Dim QRCD as New QRCode]
我收到一个错误,使用New无效,但我可以用以下行声明:
Dim QRCD as QRCode
[我认为这不是一个对象{如果我试图使它等于QRData“这是不允许的”错误}该类实现了一个具有约束的抽象类我尝试将接口放在抽象类中,我得到错误“未实现该方法”如果我把它放在子类中,我就不能创建对象,也看不到任何具有后期绑定的方法。
QRCoder库可以在这里找到。
这是提供的抽象类
{
using System;
public abstract class AbstractQRCode<T>
{
protected QRCodeData qrCodeData;
protected AbstractQRCode(QRCodeData data)
{
qrCodeData = data;
}
public abstract T GetGraphic(int pixelsPerModule);
}
这是我在VBA中的代码[这是在一个模块中]
Public Sub QRCreator(QRtext As String)
Dim QRCG As QRCoder.QRCodeGenerator
Set QRCG = New QRCoder.QRCodeGenerator
Dim QRCD As QRCodeData
Set QRCD = QRCG.CreateQRCode(QRtext, ECCLevel_Q, False)
Dim QRCO As QRCode
Set QRCO = Factory.CreateQRCode(QRCD)
Forms!Formulario1.[Oleobject].Picture = QRCO.GetGraphic(5)
End Sub
Public Sub InitiateProperties(Data As QRCodeData)
//I declared it as Variant since QRCode is not avaliable
Dim m_data As Variant
m_data = Data
End Sub
这在另一个模块中[我使用此模块创建带有参数的对象]
如何将其修改为可从microsoft access 2013使用?
还有一种没有接口的方法吗?
access是否支持vba中的位图?
我对这些东西都很陌生,非常感谢
编辑:
我无法让它工作的原因是COM VISIBLE不支持带参数的构造函数和带重写的方法。答案是我如何创建图像。由于类没有默认构造函数,因此无法创建COM对象。您不应该试图公开原始API,而应该创建一个具有符合您需要的方法的类: VBA用法:
Public Sub QRCreator(text As String)
Dim qrc As New QRCoder.QRCodeGenerator
Forms!Formulario1.[Oleobject].Picture = qrc.Create(text, CCLevel_Q, 5)
End Sub
.Net:
[Guid("C7CC4CA0-813A-431E-B92C-842A07735E72")]
[ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface _QRCodeGenerator {
public IStdPicture Create(string text, int cclevel, int pixelsPerModule);
}
[ProgId("QRCoder.QRCodeGenerator")]
[Guid("4DC2C1F8-2727-4120-80E1-8475650D8547")]
[ComVisible(true), ClassInterface(ClassInterfaceType.None)]
[Description("...")]
public class QRCodeGenerator : _QRCodeGenerator, IDisposable {
private QRCoder.QRCodeGenerator instance;
public QRCodeGenerator() {
instance = new QRCoder.QRCodeGenerator();
}
public IStdPicture Create(string text, int cclevel, int pixelsPerModule){
var qrCodeData = instance.CreateQrCode(text, cclevel);
var qrCode = new QRCoder.QRCode(qrCodeData);
var bitmap = qrCode.GetGraphic(pixelsPerModule);
return ImageToPicture(bitmap);
}
public void Dispose() {
instance.Dispose();
}
private static IStdPicture ImageToPicture(Bitmap bitmap) {
...
}
}
在检查、尝试和失败之后,我决定改变我的观点。所以我做了这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Interop;
using System.Runtime.InteropServices;
using stdole;
namespace QRCoder
{
[Guid("52724C82-F18C-460B-B48D-1F19E016F86E")]
[ComVisible (true) , InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IQRCodeGene
{
string Create(string text, QRCodeGenerator.ECCLevel value, int pixelsPerModule);
}
[Guid("4F445AA5-D642-438B-A69A-429D621A3CB0")]
[ComVisible (true), ClassInterface(ClassInterfaceType.None)]
public class QRCodeGene: IQRCodeGene, IDisposable
{
private QRCodeGenerator Instance;
public QRCodeGene()
{
Instance = new QRCodeGenerator();
}
public string Create(string text, QRCodeGenerator.ECCLevel value, int pixelsPerModule)
{
var qrCodeData = Instance.CreateQrCode(text, value);
var qrCode = new QRCode(qrCodeData);
var bitmap = qrCode.GetGraphic(pixelsPerModule);
// This line is the only modified by the provided in the code above.
bitmap.Save("C:\\"+text+".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
//I return this string for testing. I guess If removed the text wouldn't work.
return ("Hello");
}
public void Dispose()
{
Instance.Dispose();
}
}
}
上面的代码生成我将通过access VBA中的此函数发送的任何内容的QR代码:
Public Sub QR(Text As String)
Dim QRC As New QRCodeGene
Dim x As String
x = QRC.Create(Text, ECCLevel_Q, 5)
End Sub
之后,我只加载由和access.image控制源生成的图片。
感谢Florent B.提供此代码。是否需要将位图转换为IStdPciture?或者我可以直接使用位图吗?VBA不支持位图格式和
[Oleobject].Picture
需要一个实现IPictureDisp
或IPictureDisp
接口的对象。我找不到应该使用哪个库来实现“IPictureDisp”接口我找到了一些对某个StdOle.dll的引用我在项目中添加了对它的引用,然后将此指令“using StdOle;”并修改了这行“class_QRCodeGenerator:i_QRCodeGenerator,IDispatch”,我仍然得到曲线红色的线条xD。我不太擅长编程,我正在路上学习。请看下面的示例,将位图转换为IStdPicture图片:我得到错误:2192。使用此代码,将qr变为新的QRCoder.QRCodeGenerator Me.Oleobject.PictureData=qr.createqrcode(Texto1,ECCLevel.ECCLevel_Q,5),如果我更改.PictureData for.picture,它说无法打开QRCoder.QRCodeData。
[Guid("C7CC4CA0-813A-431E-B92C-842A07735E72")]
[ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface _QRCodeGenerator {
public IStdPicture Create(string text, int cclevel, int pixelsPerModule);
}
[ProgId("QRCoder.QRCodeGenerator")]
[Guid("4DC2C1F8-2727-4120-80E1-8475650D8547")]
[ComVisible(true), ClassInterface(ClassInterfaceType.None)]
[Description("...")]
public class QRCodeGenerator : _QRCodeGenerator, IDisposable {
private QRCoder.QRCodeGenerator instance;
public QRCodeGenerator() {
instance = new QRCoder.QRCodeGenerator();
}
public IStdPicture Create(string text, int cclevel, int pixelsPerModule){
var qrCodeData = instance.CreateQrCode(text, cclevel);
var qrCode = new QRCoder.QRCode(qrCodeData);
var bitmap = qrCode.GetGraphic(pixelsPerModule);
return ImageToPicture(bitmap);
}
public void Dispose() {
instance.Dispose();
}
private static IStdPicture ImageToPicture(Bitmap bitmap) {
...
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Interop;
using System.Runtime.InteropServices;
using stdole;
namespace QRCoder
{
[Guid("52724C82-F18C-460B-B48D-1F19E016F86E")]
[ComVisible (true) , InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IQRCodeGene
{
string Create(string text, QRCodeGenerator.ECCLevel value, int pixelsPerModule);
}
[Guid("4F445AA5-D642-438B-A69A-429D621A3CB0")]
[ComVisible (true), ClassInterface(ClassInterfaceType.None)]
public class QRCodeGene: IQRCodeGene, IDisposable
{
private QRCodeGenerator Instance;
public QRCodeGene()
{
Instance = new QRCodeGenerator();
}
public string Create(string text, QRCodeGenerator.ECCLevel value, int pixelsPerModule)
{
var qrCodeData = Instance.CreateQrCode(text, value);
var qrCode = new QRCode(qrCodeData);
var bitmap = qrCode.GetGraphic(pixelsPerModule);
// This line is the only modified by the provided in the code above.
bitmap.Save("C:\\"+text+".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
//I return this string for testing. I guess If removed the text wouldn't work.
return ("Hello");
}
public void Dispose()
{
Instance.Dispose();
}
}
}
Public Sub QR(Text As String)
Dim QRC As New QRCodeGene
Dim x As String
x = QRC.Create(Text, ECCLevel_Q, 5)
End Sub