简体   繁体   English

使用后期绑定从COM对象获取类/类型

[英]Get Class/Types from COM object with late binding

I have a problem with COM compatibility issues. 我有COM兼容性问题。 This link was already very helpful, COM interface Photoshop compatibility issue , explaining that COM compatibility issues can be fixed with late binding. 此链接已经非常有用, COM接口Photoshop兼容性问题 ,解释COM兼容性问题可以通过后期绑定修复。 I've tried this but I am now confused on how to use types that I used to access via the COM object. 我试过这个,但我现在很困惑如何使用我以前通过COM对象访问的类型。

Original code: 原始代码:

//This works with late binding
m_Application = new Photoshop.Application();
var refe = new Photoshop.ActionReference();

refe.PutProperty(m_Application.CharIDToTypeID("Prpr"), m_Application.CharIDToTypeID("NmbL"));

//This is the problem area
var ColorlendMode = Photoshop.PsBlendMode.psColorBlend;

var visibleLayers = new List<Photoshop.ArtLayer>();

Late binding code: 后期绑定代码:

//works...
dynamic m_Application = Activator.CreateInstance(Type.GetTypeFromProgID("Photoshop.Application"));
dynamic refe = Activator.CreateInstance(Type.GetTypeFromProgID("Photoshop.ActionReference"));

refe.PutProperty(m_Application.CharIDToTypeID("Prpr"), m_Application.CharIDToTypeID("NmbL"));

//PROBLEM:
//Doesn't compile
Type BlendModeType = Type.GetTypeFromProgID("Photoshop.PsBlendMode").psColorBlend;

//returns null
//Type BlendModeType = Type.GetTypeFromProgID("Photoshop.PsBlendMode");
//Type artLayerType = Type.GetTypeFromProgID("Photoshop.ArtLayer");

The compile erorr is: 编译erorr是:

'System.Type' does not contain a definition for 'psColorBlend'

So how do I get the enum PsBlendMode that I used to get with the Photoshop COM object (added as Reference in my project)? 那么如何获得我以前使用Photoshop COM对象获得的枚举PsBlendMode(在我的项目中添加为Reference)?

Try this code .. you will getting the blend mode Name as String type.. 试试这段代码..你将得到混合模式Name作为String类型..

 Public Enum EBlendModes
    None = 0
    psSoftLight = 13
    psScreen = 9
    psNormalBlend = 2
    psMultiply = 5
    psColorBurn = 6
    psDissolve = 3
    psHardMix = 26
    psHardLight = 14
    psDarken = 4
    psDifference = 18
    psColorBlend = 22
    psPinLight = 17
    psOverlay = 12
    psVividLight = 15
    psLighterColor = 27
    psLinearLight = 16
    psDarkerColor = 28
    psLinearDodge = 11
    psLinearBurn = 7
    psExclusion = 19
    psLuminosity = 23
    psSubtract = 29
    psPassThrough = 1
    psColorDodge = 10
    psDivide = 30
    psSaturationBlend = 21
    psLighten = 8
    psHue = 20
End Enum
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Try
        Dim _appref = Activator.CreateInstance(Type.GetTypeFromProgID("Photoshop.Application"))
        Dim _blndmod As EBlendModes = CInt(_appref.activedocument.Activelayer.blendmode)
        MsgBox(_blndmod.ToString)
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Result is : PsNormalBlend.... are u expecting like this............. 结果是:PsNormalBlend ....你期待这样.............

I don't think you can do what you are trying to do for enums. 我认为你不能做你想要为枚举做的事情。 The closest you can do will be to loop through the enums using reflection to find the one you want as a string. 您可以做的最接近的工作是使用反射遍历枚举,以找到您想要的字符串。

See this: http://www.codeproject.com/Tips/550160/Getting-enum-value-from-another-class-via-Reflecti 见: http//www.codeproject.com/Tips/550160/Getting-enum-value-from-another-class-via-Reflecti

Try running this and see if it prints out the value for psColorBlend : 尝试运行它,看看它是否打印出psColorBlend的值:

var fieldsArray = Type.GetTypeFromProgID("Photoshop.PsBlendMode").GetFields(BindingFlags.Public | BindingFlags.Static);

foreach (var fInfo in fieldsArray)
{
    var ulngValue = (ulong)Convert.ChangeType(fInfo.GetValue(null), typeof(ulong));
    Console.WriteLine(fInfo.Name.ToString(CultureInfo.InvariantCulture) + " : " + ulngValue.ToString(CultureInfo.InvariantCulture));
}  

Ok different idea: 好不同的想法:

Just define your own enum to represent PSColorBlen and use that 只需定义自己的枚举来表示PSColorBlen并使用它

public enum PsBlendMode
{
    psColorBlend = 22
}

var ColorlendMode = PsBlendMode.psColorBlend;

You may have to cast to an int when you pass it into a call. 将调用传递给调用时,可能必须转换为int。

See here for a list of the constant values: http://fs.mis.kuas.edu.tw/~wchsieh/photoshopy9.py 请参阅此处获取常量值列表: http//fs.mis.kuas.edu.tw/~wchsieh/photoshopy9.py

您无法获得没有任何文档的混合模式值或没有选择任何图层...您必须选择一个图层,然后您可以获取BlendMode值,否则您不能..因为BlendMode是一个“Artlayer {Interface}”属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM